Check if application is installed in registry

前端 未结 7 618
庸人自扰
庸人自扰 2020-12-01 21:07

Right now I use this to list all the applications listed in the registry for 32bit & 64. I have seen the other examples of how to check if an application is installed wi

7条回答
  •  暖寄归人
    2020-12-01 21:33

    I tried the solutions here, but they didnt work in some cases. The reason was, that my programm is 32 bit and runs on 64 bit Windows. With the solutions posted here a 32bit process can not check whether a 64 bit application is installed.

    How to access 64 bit registry with a 32 bit process

    RegistryKey.OpenBaseKey

    I modifed the solutions here to get a working one for this issue:

    Usage example

     Console.WriteLine(IsSoftwareInstalled("Notepad++"));
    

    Code

        public static bool IsSoftwareInstalled(string softwareName)
        {
            var registryUninstallPath                = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
            var registryUninstallPathFor32BitOn64Bit = @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall";
    
            if (Is32BitWindows())
                return IsSoftwareInstalled(softwareName, RegistryView.Registry32, registryUninstallPath);
    
            var is64BitSoftwareInstalled = IsSoftwareInstalled(softwareName, RegistryView.Registry64, registryUninstallPath);
            var is32BitSoftwareInstalled = IsSoftwareInstalled(softwareName, RegistryView.Registry64, registryUninstallPathFor32BitOn64Bit);
            return is64BitSoftwareInstalled || is32BitSoftwareInstalled;
        }
    
        private static bool Is32BitWindows() => Environment.Is64BitOperatingSystem == false;
    
        private static bool IsSoftwareInstalled(string softwareName, RegistryView registryView, string installedProgrammsPath)
        {
            var uninstallKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, registryView)
                                                  .OpenSubKey(installedProgrammsPath);
    
            if (uninstallKey == null)
                return false;
    
            return uninstallKey.GetSubKeyNames()
                               .Select(installedSoftwareString => uninstallKey.OpenSubKey(installedSoftwareString))
                               .Select(installedSoftwareKey => installedSoftwareKey.GetValue("DisplayName") as string)
                               .Any(installedSoftwareName => installedSoftwareName != null && installedSoftwareName.Contains(softwareName));
        }
    

提交回复
热议问题