c# - 获取 64 位 Windows 上已安装程序的列表

标签 c# registry 64-bit .net-2.0

我正在使用这段代码列出所有已安装的程序:

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/

相关文章:

c# - 在 C# 中命名同步对象以用于争用分析

c# - 未创建注册表项

c++ - 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2?

C++ 移位位

具有结构和字符串的 C# HashSet

c# - asp.net core 如何获取查询字符串参数值?

c# - Haskell 中的 OOP 样式继承

c++ - 检查 HKEY HANDLE 是否有效?

c - x64 上是否需要 int ?

activex - 是否有 MsStkPrp.dll 的 x64 版本