• C# 32应用程序获取64位操作系统注册表


    若C#的程序都是32位的,访问注册表的时候,会访问HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\, 而访问不到HKEY_LOCAL_MACHINE\SOFTWARE
    适用版本:.NET 4.0及更高版本

    public static Dictionary<string, string> GetInstalledList()
    {
        Dictionary<string, string> softList = new Dictionary<string, string>();
        try
        {
            // search in: CurrentUser
            RegistryKey localKey = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64);
            foreach (var item in GetInstallSoftList(localKey, @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"))
            {
                if (!softList.ContainsKey(item.Key))
                {
                    softList.Add(item.Key, item.Value);
                }
            }
            localKey = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry32);
            foreach (var item in GetInstallSoftList(localKey, @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"))
            {
                if (!softList.ContainsKey(item.Key))
                {
                    softList.Add(item.Key, item.Value);
                }
            }
    
            // search in: LocalMachine_32
            localKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
            foreach (var item in GetInstallSoftList(localKey, @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"))
            {
                if (!softList.ContainsKey(item.Key))
                {
                    softList.Add(item.Key, item.Value);
                }
            }
            localKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
            foreach (var item in GetInstallSoftList(localKey, @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"))
            {
                if (!softList.ContainsKey(item.Key))
                {
                    softList.Add(item.Key, item.Value);
                }
            }
    
            // search in: LocalMachine_64
            localKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
            foreach (var item in GetInstallSoftList(localKey, @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"))
            {
                if (!softList.ContainsKey(item.Key))
                {
                    softList.Add(item.Key, item.Value);
                }
            }
            localKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
            foreach (var item in GetInstallSoftList(localKey, @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"))
            {
                if (!softList.ContainsKey(item.Key))
                {
                    softList.Add(item.Key, item.Value);
                }
            }
        }
        catch (Exception ex)
        {
            logger.Error(ex);
        }
        return softList;
    }
    
    
    private static Dictionary<string, string> GetInstallSoftList(RegistryKey localKey, string subKey)
    {
        Dictionary<string, string> softList = new Dictionary<string, string>();
        RegistryKey key = localKey.OpenSubKey(subKey);
        if (key != null)
        {
            foreach (String keyName in key.GetSubKeyNames())
            {
                RegistryKey subkey = key.OpenSubKey(keyName);
                string displayName = subkey.GetValue("DisplayName") as string;
                var version = GetSoftVersion(subkey);
                if (displayName != null && !softList.ContainsKey(displayName))
                {
                    softList.Add(displayName, version == null ? "" : version.ToString().Replace(",", ".").Replace(" ", ""));
                }
            }
        }
    
        return softList;
    }
    
    
    private static string GetSoftVersion(RegistryKey key)
    {
        if (key == null)
        {
            return null;
        }
        var version = key.GetValue("DisplayVersion");
        if (version != null && !string.IsNullOrEmpty(version.ToString()))
        {
            return version.ToString();
        }
    
        var majorVersion = key.GetValue("MajorVersion");
        var minorVersion = key.GetValue("MinorVersion");
        if (majorVersion != null && minorVersion != null && !string.IsNullOrEmpty(majorVersion.ToString()) && !string.IsNullOrEmpty(minorVersion.ToString()))
        {
            return majorVersion.ToString() + "." + minorVersion;
        }
    
        majorVersion = key.GetValue("VersionMajor");
        minorVersion = key.GetValue("VersionMinor");
        if (majorVersion != null && minorVersion != null && !string.IsNullOrEmpty(majorVersion.ToString()) && !string.IsNullOrEmpty(minorVersion.ToString()))
        {
            return majorVersion.ToString() + "." + minorVersion;
        }
    
        return null;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
  • 相关阅读:
    spring boot自动装配之@ComponentScan详解
    Apache Doris tablet 副本修复的原理、流程及问题定位
    linux安装并配置git连接github
    JVM 彻底搞懂JVM内存区域及直接内存
    全球跨境电子商务大会中,跨境卖货系统优势在哪?
    Java图书借阅管理系统(含源码+论文+答辩PPT等)
    小程序进阶-inline、block和inline-block的区别与联系
    带风扇工业电脑行业分析:预计2030年全球市场规模将达到45.8亿美元
    《HelloGitHub》第 78 期
    二进制部署Docker
  • 原文地址:https://blog.csdn.net/weixin_38747097/article/details/134512282