我试图检查Access 2010是否安装在C#中,我尝试使用这个答案。
他在那里使用subString而不是Substring,也用indexOf代替IndexOf。
因此,在我的代码中,我使用Substring和IndexOf完成了它,但是当我运行它时给出了FormatException,下面是我的代码:
RegistryKey rootKey = Registry.ClassesRoot.OpenSubKey(@"Access.Application\CurVer" , false);
if (rootKey == null)
{
MessageBox.Show("Access 2010 not installed on this machine");
}
String value = rootKey.GetValue("").ToString();
int verNum = 0;
try
{
verNum = int.Parse(value.Substring(value.IndexOf("Access.Application.")));
} catch (FormatException fe)
{
MessageBox.Show(fe.ToString());
}
if (value.StartsWith("Access.Application.") && verNum >= 12)
{
MessageBox.Show("Access 2010 already installed on this machine");
}发布于 2018-04-03 10:27:53
在地球上,你所拥有的是不可能工作的(只是说说而已)。
你显然从这里得到了这个代码或者一些导数检查是否安装了MS 2010.而且这是可怕的错误
第一
报告此实例中指定字符串第一次出现的基于零的索引。
意味着如果它找到"Access.Application.",它将返回0
二次
从此实例检索子字符串。子字符串从指定的字符位置开始,并继续到字符串的末尾。
这意味着,给定的0将返回"Access.Application.",而这不是int
最后
如果它不是int,则引发异常。
我不确定找到访问版本号的正确方法,也不确定是否安装了access。但是,如果--如果版本号确实位于"Access.Application."后面--您可能希望使用传入.的String.LastIndexOf法
至少要使用int.TryParse确保它不会抛出异常
示例
var somekey = "Access.Application.2099";
var lastIndex = somekey.LastIndexOf(".");
if (lastIndex > 0)
Console.WriteLine("We have a chance");
var substr = somekey.Substring(lastIndex + 1);
Console.WriteLine(substr);
int verNum = 0;
if (int.TryParse(substr, out verNum))
{
Console.WriteLine("found a version maybe : " + verNum);
}
else
{
Console.WriteLine("No cigar");
}https://stackoverflow.com/questions/49627246
复制相似问题