Check if application is installed in registry

前端 未结 7 615
庸人自扰
庸人自扰 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:30

    I have checked @Stellan Lindell's code and it doesn't work in all cases. My version should work in all scenarios and checks the specific version of installed programs(x86, x64).

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Microsoft.Win32;
    
    namespace Test
    {  
    internal class Program
    {  
        public enum ProgramVersion
        {
            x86,
            x64
        }
    
        private static IEnumerable GetRegisterSubkeys(RegistryKey registryKey)
        {
            return registryKey.GetSubKeyNames()
                    .Select(registryKey.OpenSubKey)
                    .Select(subkey => subkey.GetValue("DisplayName") as string);
        }
    
        private static bool CheckNode(RegistryKey registryKey, string applicationName, ProgramVersion? programVersion)
        {
            return GetRegisterSubkeys(registryKey).Any(displayName => displayName != null
                                                                      && displayName.Contains(applicationName)
                                                                      && displayName.Contains(programVersion.ToString()));
        }
    
        private static bool CheckApplication(string registryKey, string applicationName, ProgramVersion? programVersion)
        {
            RegistryKey key = Registry.LocalMachine.OpenSubKey(registryKey);
    
            if (key != null)
            {
                if (CheckNode(key, applicationName, programVersion))
                    return true;
    
                key.Close();
            }
    
            return false;
        }
    
        public static bool IsSoftwareInstalled(string applicationName, ProgramVersion? programVersion)
        {
            string[] registryKey = new [] {
                @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall",
                @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
            };
    
            return registryKey.Any(key => CheckApplication(key, applicationName, programVersion));
        }
    
        private static void Main()
        {
            // Examples
            Console.WriteLine("Notepad++: " + IsSoftwareInstalled("Notepad++", null));
            Console.WriteLine("Notepad++(x86): " + IsSoftwareInstalled("Notepad++", ProgramVersion.x86));
            Console.WriteLine("Notepad++(x64): " + IsSoftwareInstalled("Notepad++", ProgramVersion.x64));
            Console.WriteLine("Microsoft Visual C++ 2009: " + IsSoftwareInstalled("Microsoft Visual C++ 2009", null));
            Console.WriteLine("Microsoft Visual C-- 2009: " + IsSoftwareInstalled("Microsoft Visual C-- 2009", null));
            Console.WriteLine("Microsoft Visual C++ 2013: " + IsSoftwareInstalled("Microsoft Visual C++ 2013", null));
            Console.WriteLine("Microsoft Visual C++ 2012 Redistributable (x86): " + IsSoftwareInstalled("Microsoft Visual C++ 2013", ProgramVersion.x86));
            Console.WriteLine("Microsoft Visual C++ 2012 Redistributable (x64): " + IsSoftwareInstalled("Microsoft Visual C++ 2013", ProgramVersion.x64));
            Console.ReadKey();
        }
    }
    }
    

提交回复
热议问题