【问题标题】:How to access 64-bit HKLM\Software Registry with a 32-bit C#.NET Application如何使用 32 位 C#.NET 应用程序访问 64 位 HKLM\Software Registry
【发布时间】:2020-11-04 13:33:50
【问题描述】:

所以我需要读取 PC 上安装的所有应用程序,但问题是其中一部分在 64 位注册表值中,因此我无法使用 32 位应用程序读取它。

所以我需要将其设置为读取 64 位值,这样我就可以使用我的 32 位应用程序读取 64 位注册表值。我将在这里展示我的例子。

【问题讨论】:

  • 在自我回答时(这是完全有效的),您应该将问题作为问题阅读。 “解决方案很简单”不是您希望在问题开头找到的句子。
  • 注意,我会编辑它。谢谢:)

标签: c# registry 64-bit


【解决方案1】:

这是我在注册表中读取所有 32 位应用程序的方式:

string uninstallKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(uninstallKey))
        {
            foreach (string skName in rk.GetSubKeyNames())
            {
                using (RegistryKey sk = rk.OpenSubKey(skName))
                {
                    try
                    {
                        var displayName = sk.GetValue("DisplayName");
                        var size = sk.GetValue("EstimatedSize");

                        ListViewItem item;
                        if (displayName != null)
                        {
                            if (size != null)
                                item = new ListViewItem(new string[] { displayName.ToString(), size.ToString(), rk.OpenSubKey(skName).ToString() });
                            else
                                item = new ListViewItem(new string[] { displayName.ToString(), null, rk.OpenSubKey(skName).ToString() });
                            listInstalled.Items.Add(item);
                        }
                    }
                    catch (Exception ex)
                    { }
                }
            }
        }

这就是我在注册表中读取所有 64 位应用程序的方式:

string uninstallKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
RegistryKey regkey64 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
RegistryKey rk64 = regkey64.OpenSubKey(uninstallKey);
foreach (string skName in rk64.GetSubKeyNames())
{
        using (RegistryKey sk = rk64.OpenSubKey(skName))
        {
                try
                {
                    var displayName = sk.GetValue("DisplayName");
                    var size = sk.GetValue("EstimatedSize");

                    ListViewItem item;
                    if (displayName != null)
                    {
                        if (size != null)
                            item = new ListViewItem(new string[] { displayName.ToString(), size.ToString(), rk64.OpenSubKey(skName).ToString() });
                        else
                            item = new ListViewItem(new string[] { displayName.ToString(), null, rk64.OpenSubKey(skName).ToString() });
                        listInstalled.Items.Add(item);
                    }
                }
                catch (Exception ex)
                { }
        }
}

如果您没有注意到这些差异,那么这次它们以 BOLD 显示:

32 位:RegistryKey rk = Registry.LocalMachine.OpenSubKey(uninstallKey)

64 位:RegistryKey regkey64 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64); RegistryKey rk64 = regkey64.OpenSubKey(uninstallKey);

虽然它记录在 Microsoft 的网站上,但我还是决定在这里写它,因为这给我带来了很多麻烦。而您无法读取它的原因仅仅是因为它是一个 64 位值(32 位应用程序无法定期访问它 - 以科学的名义确认您自己;仅使用 32-位代码,然后按 Alt-Enter>Build>取消选中“Prefer 32-bit”。现在再次运行它,您会看到它显示所有 64 位应用程序。

我希望这会很有用。您可以在此处查看“技巧”并将其应用于您的代码。 gl hf :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-15
    • 2014-07-18
    • 2013-04-11
    • 1970-01-01
    • 2013-07-24
    • 2011-10-27
    相关资源
    最近更新 更多