我正在尝试检查是否使用 C# 安装了 Access 2010,我尝试使用此答案。
他使用那里subString
代替Substring
也indexOf
代替IndexOf
。
所以在我的代码中,我用Substring
and完成了它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");
}
地球上没有办法你所拥有的东西会起作用(只是说)
您显然已经从这里或一些衍生产品获得了此代码检查是否安装了 MS Access 2010 ......而且它是非常错误的
首先
报告此实例中第一次出现指定字符串的从零开始的索引
这意味着它会返回 0 如果它找到 "Access.Application."
第二
从此实例中检索子字符串。子字符串从指定的字符位置开始,一直到字符串的末尾。
这意味着,给定 0 将返回"Access.Application."
,这不是int
最后
如果不是,则抛出异常 int
我不确定找到访问版本号的正确方法或如何检测是否安装了访问。然而,如果版本号真正的生活背后"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");
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句