我编写了一个 C# 代码来查找特定版本是否安装在机器中。当我通过调试测试此代码时,它按我的预期工作。
但是,当我通过 N 单元传递参数并运行断言时,我得到了错误的输出。我怀疑 N-Unit 没有访问注册表项。
谁能提供您对此的建议并指导我走正确的道路?
以下是我的 N 单元代码
[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;
}
提前致谢!!!