Home / Programming / Blog article: Get A List Of Installed Applications Using LINQ And C#

| RSS

Get A List Of Installed Applications Using LINQ And C#

August 29th, 2008 | 13 Comments | Posted in Programming

To get a list of installed applications we need to look into registry. Microsoft.Win32 namespace contains objects which can be used to work with Windows Registry. In this post I will show you some code where I use the power of LINQ to retrieve and display a list of all applications installed on a machine.

The basic idea is that we iterate through a collection of RegistryKey objects within LocalMachine\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall. We then open the sub keys and get the DisplayName.

Here is the code:

static void DisplayInstalledApplications()
{
  string registryKey = 
    @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
 
  using (Microsoft.Win32.RegistryKey key =
    Registry.LocalMachine.OpenSubKey(registryKey))
  {
    var query = from a in
              key.GetSubKeyNames()
              let r = key.OpenSubKey(a)
              select new
              {
                Application = r.GetValue("DisplayName")
              };
 
    foreach (var item in query)
    {
      if (item.Application != null)
        Console.WriteLine(item.Application);
    }
  }
}
 
I can also make this a bit more LINQed by removing the foreach loop. It just adds a bit more C# 3.0 flavour to the code and does the retrieval and writing to console in one line.
 
static void DisplayInstalledApplications2()
{
  string registryKey =
    @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
 
  using (Microsoft.Win32.RegistryKey key = 
    Registry.LocalMachine.OpenSubKey(registryKey))
  {
    (from a in key.GetSubKeyNames()
    let r = key.OpenSubKey(a)
    select new
    {
      Application = r.GetValue("DisplayName")
    }).ToList()
      .FindAll(c => c.Application != null)
      .ForEach(c => Console.WriteLine(c.Application));
  }
}
 
I hope you found this post useful. If yes, then do not forget to subscribe to OneDotNetWay Feed .

kick it on DotNetKicks.com

Technorati Tags: ,






Leave a Reply 6600 views, 18 so far today |
Tags: ,
Follow Discussion

13 Responses to “Get A List Of Installed Applications Using LINQ And C#”

  1. Rajesh Says:

    This is exactly what I was looking for. And getting the list using LINQ makes it even better.

    Looks like you have just changed the look of your site. It looks more professional now.

  2. Deepak Says:

    Rajesh,

    I am glad that you found this post useful. Thank you for your comment on the new theme. I was looking for something which would allow me to give this site a portal-type look. Finally I found the right theme.

  3. Mr.Bhatt Says:

    thats a lot for this helpful code snippet… save so much of my time, since i am beginner to programming…

    thanks again Deepak,

    anil bhatt…

  4. Deepak Says:

    Anil,

    I am happy to know that this post helped you.

  5. Taka Says:

    Excellent snippet!

  6. Deepak Says:

    Thanks Taka.

  7. Art Says:

    Such way you don’t get applications that are installed for the specific user. You need to look in one more place in the registry:
    HKEY_USERS\S-1-5-21-…-7206\Software\Microsoft\Windows\CurrentVersion\Uninstall
    for every user that exists in the OS

  8. NT Says:

    Or, much more compact:

    string registryKey = @”SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall”;

    RegistryKey key = LocalMachine.OpenSubKey(registryKey);
    foreach (String a in key.GetSubKeyNames()) {
    RegistryKey subkey = key.OpenSubKey(a);
    Console.WriteLine(subkey.GetValue(”DisplayName”));
    }

  9. Deepak Says:

    nice one NT

  10. skroslak Says:

    for 64bit OS you also need to search this key:
    “SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall”

  11. skroslak Says:

    so the whole code that works for me is here (may be simplified, I don’t know):
    string registryKey = @”SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall”;
    RegistryKey key = Registry.LocalMachine.OpenSubKey(registryKey);
    foreach (String a in key.GetSubKeyNames())
    {
    RegistryKey subkey = key.OpenSubKey(a);
    Console.WriteLine(subkey.GetValue(”DisplayName”));
    }
    registryKey = @”SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall”;
    key = Registry.LocalMachine.OpenSubKey(registryKey);
    foreach (String a in key.GetSubKeyNames())
    {
    RegistryKey subkey = key.OpenSubKey(a);
    Console.WriteLine(subkey.GetValue(”DisplayName”));
    }

  12. Deepak Says:

    Thanks Skroslak.

Trackbacks

  1. Channel 9 Visits One .Net Way | One .Net Way  

Leave a Reply