views:

1428

answers:

6

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

A: 

Your best bet is to use WMI. Specifically the Win32_Product class.

paxdiablo
A: 

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

Moayad Mardini
+2  A: 

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)
                { }
            }
        }
    }
}
Kirtan
A: 

Might I suggest you take a look at WMI (Windows Management Instrumentation). If you add the System.Management reference to your C# project, you'll gain access to the class `ManagementObjectSearcher', which you will probably find useful.

There are various WMI Classes for Installed Applications, but if it was installed with Windows Installer, then the Win32_Product class is probably best suited to you.

ManagementObjectSearcher s = new ManagementObjectSearcher("SELECT * FROM Win32_Product");
Nick
+4  A: 

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 version using Linq here and 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.

Xiaofu
It's worth noting that using the WMI Win32_Product class is a bad idea if you plan to run this query repeatedly. See this Microsoft KB article: http://support.microsoft.com/kb/974524/EN-USThe core problem is the (a) Win32_Product is really slow and (b) it generates a "Windows Installer reconfigured the product." event log message for *every* installed product on your system... every time you run the query. Doh!This article recommends using the Win32reg_AddRemovePrograms class... which isn't present unless you have installed SMS. Doh!So probably better to stick with the registry query.
Simon Gillbee
Simon Gillbee's comment should be the accepted answer, or Kirtans!WMI WIN32_Product is not the way to go here, trust me!
Paperino
Er, that's why the registry example is first in my answer. WMI was presented simply as an alternative solution, and even there I state "this is rather slower to execute" and other drawbacks. Read the answer from the beginning. ;)
Xiaofu
A: 

Here is a complete code for searching for Installed application or a program. 100% work on 32 and 64 bit os!!!

http://cf-sami.blogspot.com/2010/09/c-check-if-programapplication-is.html

cf Blogpost
What about rude applications that don't write their uninstall info to the registry? (they still exist, unfortunately)
Piskvor