How to Get a List of Installed Applications with LINQ

One of the powerful features of LINQ is that it can be used on any List<> or IENumerable collection. Retrieving a list of installed applications on a computer using the C# 2.0 method involves several lines of code (20 in the example below), while the LINQ method results in 10 lines of code (a 50% reduction). Not only is the LINQ method more concise, it executes faster, as well.

Print List of Installed Applications Using C# 2.0 Method

 

const string registryKey = @"SOFTWAREMicrosoftWindowsCurrentVersionUninstall";

using (RegistryKey key = Registry.LocalMachine.OpenSubKey(registryKey))
{
    foreach (string keyName in key.GetSubKeyNames())
    {
        // open the registry key for this application
        RegistryKey ApplicationKey = key.OpenSubKey(keyName);

        // skip if no display name
        if (ApplicationKey.GetValue("DisplayName") == null)
            continue;

        // get the application name
        string ApplicationName = ApplicationKey.GetValue("DisplayName").ToString();
       
        // output the result
        Console.WriteLine(ApplicationName);
    }
}

 

As you can see, just to get the list of installed apps can take some work. LINQ makes it easy:

Print List of Installed Applications Using LINQ

const string registryKey = @"SOFTWAREMicrosoftWindowsCurrentVersionUninstall";

using (RegistryKey key = Registry.LocalMachine.OpenSubKey(registryKey))
{
    key.GetSubKeyNames()
        .Select(x => key.OpenSubKey(x).GetValue("DisplayName"))
        .Where(x=> x != null)
        .ToList()
        .ForEach(Console.WriteLine);
}

 

The end result is the same, but with fewer lines of code, and faster execution time.

GD Star Rating
loading...
GD Star Rating
loading...
  • Share/Bookmark

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

Leave a Reply