Читайте программное обеспечение Windows из реестра - PullRequest
0 голосов
/ 28 октября 2019

У меня есть программа ac #, которая читает установленное программное обеспечение Windows с RegKey SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall

Это само по себе на моей Windows 10 64-битной машине работает нормально. Но когда я тестирую его, например, на Windows 7, 32-битный или Windows 7, 64-битный, он не работает. Я получаю System.NullReferenceException.

Вот код, в котором я пытаюсь прочитать Установленное программное обеспечение:

            List<WindowsSoftware> windowsSoftware = new List<WindowsSoftware>();
            try
            {
                RegistryKey baseKey;
                RegistryKey key;

                //HKCU x86 (CurrentUser, Registry32)
                baseKey = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry32);
                key = baseKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall");
                foreach (string keyName in key.GetSubKeyNames())
                {
                    RegistryKey subkey = key.OpenSubKey(keyName);

                    WindowsSoftware software = new WindowsSoftware
                    {
                        Publisher = subkey.GetValue("Publisher") as string,
                        DisplayName = subkey.GetValue("DisplayName") as string,
                        DisplayVersion = subkey.GetValue("DisplayVersion") as string,
                        InstallLocation = subkey.GetValue("InstallLocation") as string,
                        IntsallSource = subkey.GetValue("IntsallSource") as string,
                        InstallDate = subkey.GetValue("InstallDate") as string,
                        UninstallString = subkey.GetValue("UninistallString") as string
                    };

                    if (software.Publisher == null)
                        software.Publisher = "Unknown Publisher";

                    if (software.DisplayVersion != null)
                    {
                        windowsSoftware.Add(software);
                    }
                }

                //HKLM x86 (LocalMachine, Registry32)
                baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
                key = baseKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall");
                foreach (string keyName in key.GetSubKeyNames())
                {
                    RegistryKey subkey = key.OpenSubKey(keyName);

                    WindowsSoftware software = new WindowsSoftware
                    {
                        Publisher = subkey.GetValue("Publisher") as string,
                        DisplayName = subkey.GetValue("DisplayName") as string,
                        DisplayVersion = subkey.GetValue("DisplayVersion") as string,
                        InstallLocation = subkey.GetValue("InstallLocation") as string,
                        IntsallSource = subkey.GetValue("IntsallSource") as string,
                        InstallDate = subkey.GetValue("InstallDate") as string,
                        UninstallString = subkey.GetValue("UninistallString") as string
                    };

                    if (software.Publisher == null)
                        software.Publisher = "Unknown Publisher";

                    if (software.DisplayVersion != null)
                    {
                        windowsSoftware.Add(software);
                    }
                }

                //HKLM x64 (LocalMachine, Registry64)
                baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
                key = baseKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall");
                foreach (string keyName in key.GetSubKeyNames())
                {
                    RegistryKey subkey = key.OpenSubKey(keyName);

                    WindowsSoftware software = new WindowsSoftware
                    {
                        Publisher = subkey.GetValue("Publisher") as string,
                        DisplayName = subkey.GetValue("DisplayName") as string,
                        DisplayVersion = subkey.GetValue("DisplayVersion") as string,
                        InstallLocation = subkey.GetValue("InstallLocation") as string,
                        IntsallSource = subkey.GetValue("IntsallSource") as string,
                        InstallDate = subkey.GetValue("InstallDate") as string,
                        UninstallString = subkey.GetValue("UninstallString") as string
                    };

                    if (software.Publisher == null)
                        software.Publisher = "Unknown Publisher";

                    if (software.DisplayName != null)
                    {
                        windowsSoftware.Add(software);
                    }
                }

                windowsSoftware.Sort((x, y) => x.Publisher.CompareTo(y.Publisher));
            }
            catch (Exception ex)
            {
                log.Error($"Failed to get Windows software from registry.", ex);
            }

            return windowsSoftware;

Любая помощь очень ценится.

...