检查是否安装了2010年的访问会带来充满活力



我正在尝试检查是否使用C#安装的访问,我尝试使用此答案。

他在那里使用subString,而不是Substring,而不是indexOf而不是IndexOf

所以在我的代码中,我使用SubstringIndexOf进行了操作,但是当我运行时,它给出了FormatException,这是我的代码:

RegistryKey rootKey = Registry.ClassesRoot.OpenSubKey(@"Access.ApplicationCurVer" , 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的一些派生检查...

首先

string.indexof

报告了指定的第一次出现的基于零的索引 在此实例中的字符串

表示如果找到"Access.Application."

,它将返回0

其次

string.substring

从此实例中检索一个子字符串。子字符串从 指定的字符位置并继续到字符串的末端。

这意味着,给定0将返回"Access.Application.",这不是int

最后

int32.parse

如果不是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");
}

演示此处

最新更新