How to check with C# where a program is installed

后端 未结 4 1693
一生所求
一生所求 2021-01-12 06:47

I need to check where a program is installed by program name (name that appears in Add or Remove Programs). What is the best way to that so that it\'d work fine for all lang

相关标签:
4条回答
  • 2021-01-12 07:07

    Take a look into the registry at

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
    

    Just iterate over all subkeys and take a look into the values DisplayName and InstallLocation. Here you'll find the infos you want and much more ;-)

    0 讨论(0)
  • 2021-01-12 07:08

    You can achieve this using WMI Classes. But the prerequisite is

    • the application and must be running

    below the sample code to do this

     string queryString = 
                    "SELECT Name, ProcessId, Caption, ExecutablePath" + 
                    "  FROM Win32_Process";
    
                SelectQuery query = new SelectQuery(queryString);
                ManagementScope scope = new System.Management.ManagementScope(@"\\.\root\CIMV2");
    
                ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
                ManagementObjectCollection processes = searcher.Get();
    
    
                foreach(ManagementObject mObj in processes)
                {
                                   var name = mObj ["Name"].ToString();
                                    var ProcessId = Convert.ToInt32(mObj ["ProcessId"]);
                                    var Caption = mObj ["Caption"].ToString();
                                    var Path = mObj ["ExecutablePath"].ToString();
                }
    
    0 讨论(0)
  • 2021-01-12 07:22

    To add to Oliver's answer I have wrapped up this check inside a static method.

    public static bool IsProgramInstalled(string programDisplayName) {
    
        Console.WriteLine(string.Format("Checking install status of: {0}",  programDisplayName));
        foreach (var item in Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall").GetSubKeyNames()) {
    
            object programName = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\" + item).GetValue("DisplayName");
    
            Console.WriteLine(programName);
    
            if (string.Equals(programName, programDisplayName)) {
                Console.WriteLine("Install status: INSTALLED");
                return true;
            }
        }
        Console.WriteLine("Install status: NOT INSTALLED");
        return false;
    }
    
    0 讨论(0)
  • 2021-01-12 07:23

    Have a look at these links

    Using Windows Installer to Inventory Products and Patches

    and

    MsiGetProductInfoEx Function

    0 讨论(0)
提交回复
热议问题