NUnit-如何确保在系统中安装软件版本?
我已经写了一个C代码来查找特定版本是否安装在机器上。当我通过调试来测试这段代码时,它就像我预期的那样工作 但是,当我通过N-unit传递参数并运行断言时,我得到了错误的输出。我怀疑N-Unit没有访问注册表项 有谁能提供你的建议,并引导我走上正确的道路 下面是我的N单元代码NUnit-如何确保在系统中安装软件版本?,nunit,nunit-2.5,nunit-console,Nunit,Nunit 2.5,Nunit Console,我已经写了一个C代码来查找特定版本是否安装在机器上。当我通过调试来测试这段代码时,它就像我预期的那样工作 但是,当我通过N-unit传递参数并运行断言时,我得到了错误的输出。我怀疑N-Unit没有访问注册表项 有谁能提供你的建议,并引导我走上正确的道路 下面是我的N单元代码 [Test] public void IsSTDInstalled() { Assert.IsTrue(GetInstalledDetail.IsInstalled("
[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;
}
提前感谢 您需要发布IsInstalled的代码,因为这可能是问题所在。@PatrickQuirk。。我已经更新了IsInstalled方法代码。请仔细阅读并提供您的建议。当代码在NUnit下运行时,您是否调试过该代码以查看失败的地方?它是如何失败的?