Chat LIVE With Programming Experts! There Are 23 Online Right Now...

 

Code Snippets

  

C# Source Code


Welcome to Dream.In.Code
Become a C# Expert!

Join 244,179 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,345 people online right now. Registration is fast and FREE... Join Now!





Get a list of installed software in C#

Gets a list of installed software and, if known, the software's install path.

Submitted By: aj32
Actions:
Rating:
Views: 8,444

Language: C#

Last Modified: May 31, 2008
Instructions: 1. Add 'Using Microsoft.Win32' at the top of your code.
2. Put the following code into a class.
3. See example on using the code.

Snippet


  1. /// <summary>
  2.         /// Gets a list of installed software and, if known, the software's install path.
  3.         /// </summary>
  4.         /// <returns></returns>
  5.         private string Getinstalledsoftware()
  6.         {
  7.             //Declare the string to hold the list:
  8.             string Software = null;
  9.  
  10.             //The registry key:
  11.             string SoftwareKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
  12.             using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(SoftwareKey))
  13.             {
  14.                 //Let's go through the registry keys and get the info we need:
  15.                 foreach (string skName in rk.GetSubKeyNames())
  16.                 {
  17.                     using (RegistryKey sk = rk.OpenSubKey(skName))
  18.                     {
  19.                         try
  20.                         {
  21.                             //If the key has value, continue, if not, skip it:
  22.                             if (!(sk.GetValue("DisplayName") == null))
  23.                             {
  24.                                 //Is the install location known?
  25.                                 if (sk.GetValue("InstallLocation") == null)
  26.                                     Software += sk.GetValue("DisplayName") + " - Install path not known\n"; //Nope, not here.
  27.                                 else
  28.                                     Software += sk.GetValue("DisplayName") + " - " + sk.GetValue("InstallLocation") + "\n"; //Yes, here it is...
  29.                             }
  30.                         }
  31.                         catch (Exception ex)
  32.                         {
  33.                             //No, that exception is not getting away... :P
  34.                         }
  35.                     }
  36.                 }
  37.             }
  38.  
  39.             return Software;
  40.         }
  41.  
  42.  
  43. //EXAMPLE USAGE:
  44. private void get_software_list_button__Click(object sender, EventArgs e)
  45.         {
  46.             MessageBox.Show(Getinstalledsoftware());
  47.         }

Copy & Paste


Comments


smcherniss 2008-06-25 12:19:13

AJ32 - Great Code thank you for posting. Here is the same thing in VB Imports Microsoft.Win32 Namespace ApplicationPackageHunter Public Class ApplicationPackageHunter ' ' Gets a list of installed software and, if known, the software's install path. ' ' List of Applications (String) Public Function RetrieveInstalledSoftwareList() As String ' Declare the string to hold the list: Dim Software As String = String.Empty ' The registry key: Dim SoftwareKey As String = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" Using rk As RegistryKey = Registry.LocalMachine.OpenSubKey(SoftwareKey) ' Let's go through the registry keys and get the info we need: Dim skName As String For Each skName In rk.GetSubKeyNames() Using sk As RegistryKey = rk.OpenSubKey(skName) Try ' If the key has value, continue, if not, skip it: If (Not (sk.GetValue("DisplayName") = String.Empty)) Then ' Is the install location known? If (sk.GetValue("InstallLocation") = String.Empty) Then Software += sk.GetValue("DisplayName") & _ " - Install path not known\n" ' Nope, not here. End If Else Software += sk.GetValue("DisplayName") & " - " & _ sk.GetValue("InstallLocation") + "\n" ' Yes, here it is... End If Catch ex As Exception ' No, that exception is not getting away... :P End Try End Using Next skName End Using Return Software End Function End Class End Namespace


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month