我正在使用这段代码列出所有已安装的程序:
object line;
string registry_key = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
using (Microsoft.Win32.RegistryKey key = Registry.LocalMachine.OpenSubKey(registry_key))
{
foreach (string subkey_name in key.GetSubKeyNames())
{
using (RegistryKey subkey = key.OpenSubKey(subkey_name))
{
line = subkey.GetValue("DisplayName");
if (line != null)
{
listBox1.Items.Add(line);
}
}
}
}
在 64 位 Windows 上,这将重定向到 Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall。但部分程序入口仍位于原路径,列表不完整。如何避免重定向并从 64 位 Windows 安装中的两个路径读取值(在 32 位 Windows 中仅读取第一个路径)?
最佳答案
将您的代码更改为以下内容:
object line;
string registry_key = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
using (var baseKey = Microsoft.Win32.RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
{
using (var key = baseKey.OpenSubKey(registry_key))
{
foreach (string subkey_name in key.GetSubKeyNames())
{
using (var subKey = key.OpenSubKey(subkey_name))
{
line = subKey.GetValue("DisplayName");
if (line != null)
{
listBox1.Items.Add(line);
}
}
}
}
}
你可以指定
RegistryView.Registry64
或
RegistryView.Registry32
明确地,而不是让它默认为它喜欢的任何东西。
关于c# - 获取 64 位 Windows 上已安装程序的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25307650/