Get installed applications in a system

后端 未结 14 994
遥遥无期
遥遥无期 2020-11-22 08:28

How to get the applications installed in the system using c# code?

相关标签:
14条回答
  • 2020-11-22 09:07

    You can take a look at this article. It makes use of registry to read the list of installed applications.

    public void GetInstalledApps()
    {
        string uninstallKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
        using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(uninstallKey))
        {
            foreach (string skName in rk.GetSubKeyNames())
            {
                using (RegistryKey sk = rk.OpenSubKey(skName))
                {
                    try
                    {
                        lstInstalled.Items.Add(sk.GetValue("DisplayName"));
                    }
                    catch (Exception ex)
                    { }
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 09:09

    Use Windows Installer API!

    It allows to make reliable enumeration of all programs. Registry is not reliable, but WMI is heavyweight.

    0 讨论(0)
  • 2020-11-22 09:11

    it's worth noting that the Win32_Product WMI class represents products as they are installed by Windows Installer. not every application use windows installer

    however "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" represents applications for 32 bit. For 64 bit you also need to traverse "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" and since not every software has a 64 bit version the total applications installed are a union of keys on both locations that have "UninstallString" Value with them.

    but the best options remains the same .traverse registry keys is a better approach since every application have an entry in registry[including the ones in Windows Installer].however the registry method is insecure as if anyone removes the corresponding key then you will not know the Application entry.On the contrary Altering the HKEY_Classes_ROOT\Installers is more tricky as it is linked with licensing issues such as Microsoft office or other products. for more robust solution you can always combine registry alternative with the WMI.

    0 讨论(0)
  • 2020-11-22 09:12

    Iterating through the registry key "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" seems to give a comprehensive list of installed applications.

    Aside from the example below, you can find a similar version to what I've done here.

    This is a rough example, you'll probaby want to do something to strip out blank rows like in the 2nd link provided.

    string registry_key = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
    using(Microsoft.Win32.RegistryKey key = Registry.LocalMachine.OpenSubKey(registry_key))
    {
        foreach(string subkey_name in key.GetSubKeyNames())
        {
            using(RegistryKey subkey = key.OpenSubKey(subkey_name))
            {
                Console.WriteLine(subkey.GetValue("DisplayName"));
            }
        }
    }
    

    Alternatively, you can use WMI as has been mentioned:

    ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT * FROM Win32_Product");
    foreach(ManagementObject mo in mos.Get())
    {
        Console.WriteLine(mo["Name"]);
    }
    

    But this is rather slower to execute, and I've heard it may only list programs installed under "ALLUSERS", though that may be incorrect. It also ignores the Windows components & updates, which may be handy for you.

    0 讨论(0)
  • 2020-11-22 09:12

    I used Nicks approach - I needed to check whether the Remote Tools for Visual Studio are installed or not, it seems a bit slow, but in a seperate thread this is fine for me. - here my extended code:

        private bool isRdInstalled() {
            ManagementObjectSearcher p = new ManagementObjectSearcher("SELECT * FROM Win32_Product");
            foreach (ManagementObject program in p.Get()) {
                if (program != null && program.GetPropertyValue("Name") != null && program.GetPropertyValue("Name").ToString().Contains("Microsoft Visual Studio 2012 Remote Debugger")) {
                    return true;
                }
                if (program != null && program.GetPropertyValue("Name") != null) {
                    Trace.WriteLine(program.GetPropertyValue("Name"));
                }
            }
            return false;
        }
    
    0 讨论(0)
  • 2020-11-22 09:13

    Iterate through "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" keys and check their "DisplayName" values.

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