我编写了一个C#代码,发现特定版本是否安装在机器上。当我通过调试测试这个代码时,它可以正常工作。
但是,当我传递参数并通过N-unit运行断言时,我得到了错误的输出。我怀疑N-Unit没有访问注册表项。
有人可以就此提出您的建议,并指导我走正确的道路吗?
以下是我的N-Unit代码
[Test]
public void IsSTDInstalled()
{
Assert.IsTrue(GetInstalledDetail.IsInstalled("1.4.0.2"));
Assert.IsFalse(GetInstalledDetail.IsInstalled("1.4.0.3"));
}
这是IsInstalled方法代码:
public static bool IsInstalled(string version)
{
string path = string.Empty;
bool result = false;
string sLocation = "SOFTWARE\\A\\InstalledVersions";
string sLocation64bit = "SOFTWARE\\Wow6432Node\\A\\InstalledVersions";
RegistryKey key, versionKey;
key = Registry.LocalMachine.OpenSubKey(sLocation);
if (key == null)
{
key = Registry.LocalMachine.OpenSubKey(sLocation64bit);
sLocation = sLocation64bit;
}
if (key != null)
{
foreach (string isVersion in key.GetSubKeyNames())
{
if (isVersion == version)
{
versionKey = Registry.LocalMachine.OpenSubKey(sLocation + "\\" + version);
try
{
path = versionKey.GetValue("") as string;
if (Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories).Any())
{
result = true;
break;
}
else
{
result = false;
}
}
catch (Exception exception)
{
result = false;
}
}
}
}
return result;
}
提前致谢!!!