现在我用它来列出注册表中列出的所有 32 位和 64 位应用程序。 我已经看到了如何检查应用程序是否已安装的其他示例,但运气不佳。
string registryKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
RegistryKey key = Registry.LocalMachine.OpenSubKey(registryKey);
if (key != null)
{
foreach (String a in key.GetSubKeyNames())
{
RegistryKey subkey = key.OpenSubKey(a);
Console.WriteLine(subkey.GetValue("DisplayName"));
}
}
registryKey = @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall";
key = Registry.LocalMachine.OpenSubKey(registryKey);
if (key != null)
{
foreach (String a in key.GetSubKeyNames())
{
RegistryKey subkey = key.OpenSubKey(a);
Console.WriteLine(subkey.GetValue("DisplayName"));
}
}
所以这段代码在控制台窗口中列出了所有内容,而我想要做的是 只需从显示名称列表中找到一个程序标题,看看它是否已安装。
我最后尝试的是
if (subkey.Name.Contains("OpenSSL"))
Console.Writeline("OpenSSL Found");
else
Console.Writeline("OpenSSL Not Found");
我尝试的任何结果要么是假的,要么是误报。有没有人可以告诉我如何从列表中获取标题?
请不要发布众所周知的 private static void IsApplicationInstalled(p_name) 函数。它对我根本不起作用。
最佳答案
经过搜索和故障排除后,我让它以这种方式工作:
public static bool checkInstalled (string c_name)
{
string displayName;
string registryKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
RegistryKey key = Registry.LocalMachine.OpenSubKey(registryKey);
if (key != null)
{
foreach (RegistryKey subkey in key.GetSubKeyNames().Select(keyName => key.OpenSubKey(keyName)))
{
displayName = subkey.GetValue("DisplayName") as string;
if (displayName != null && displayName.Contains(c_name))
{
return true;
}
}
key.Close();
}
registryKey = @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall";
key = Registry.LocalMachine.OpenSubKey(registryKey);
if (key != null)
{
foreach (RegistryKey subkey in key.GetSubKeyNames().Select(keyName => key.OpenSubKey(keyName)))
{
displayName = subkey.GetValue("DisplayName") as string;
if (displayName != null && displayName.Contains(c_name))
{
return true;
}
}
key.Close();
}
return false;
}
我只是简单地用它来调用它
if(checkInstalled("Application Name"))
关于c# - 检查应用程序是否安装在注册表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16379143/