How to enumerate the software products that can be uninstalled on a computer (821775)



The information in this article applies to:

  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)

IN THIS TASK

IMPORTANT: This article contains information about modifying the registry. Before you modify the registry, make sure to back it up and make sure that you understand how to restore the registry if a problem occurs. For information about how to back up, restore, and edit the registry, click the following article number to view the article in the Microsoft Knowledge Base:

256986 Description of the Microsoft Windows Registry

SUMMARY

This step-by-step article describes how to enumerate the software products that can be uninstalled on a computer by examining the following registry key:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall

This article also includes a sample code listing that illustrates the concepts that are discussed in the article.

Note You may have to have administrative credentials on your computer to access certain registry keys.

Requirements

The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
  • Microsoft Windows 2000, Microsoft Windows XP, or Microsoft Windows Server 2003
  • Microsoft Visual Basic .NET or Microsoft Visual Basic 2005
This article assumes that you are familiar with the following topics:
  • Programming with Visual Basic .NET or Visual Basic 2005
  • The System Registry

Create a console application

  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. On the File menu, point to New, and then click Project.
  3. Under Project Types, click Visual Basic Projects.

    Note In Visual Studio 2005, click Visual Basic under Project Types.
  4. Under Templates, click Console Application.
  5. In the Name box, type SoftwareDemo, and then click OK. By default, Module1.vb is created.
  6. To import the Microsoft.Win32 namespace that contains classes that make changes to the system registry, add the following code at the top of the Code window:
    Imports Microsoft.Win32

Open the uninstall registry subkey

WARNING: If you use Registry Editor incorrectly, you may cause serious problems that may require you to reinstall your operating system. Microsoft cannot guarantee that you can solve problems that result from using Registry Editor incorrectly. Use Registry Editor at your own risk.

The list of installed software products is located at the following registry path:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall

To access the list of installed software products, open this registry subkey by adding the following code to the Main method:
' Open the Uninstall registry subkey.
Dim Key As RegistryKey = Registry.LocalMachine.OpenSubKey _
   ("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", False)

Retrieve a list of installed software products

To retrieve the list of installed software products as an array of strings, add the following code after the code that you added in the "Open the uninstall registry subkey" section of this article:
' Retrieve a list of installed software products.
' This list also includes some software products that are not valid.
Dim SubKeyNames() As String = Key.GetSubKeyNames()

Get rid of software products that are not valid

The list of software products that you retrieved in the previous section has some products that are not valid. To get rid of these products, get rid of those products that do not have a DisplayName value. To do this and to display the valid list of installed software products, add the following code after the code that you added in the "Retrieve a list of installed software products" section of this article:
' Declare a variable to iterate through the retrieved list of 
' installed software products.
Dim Index As Integer
' Declare a variable to hold the registry subkey that corresponds 
' to each retrieved software product.
Dim SubKey As RegistryKey
Console.WriteLine("The following software products are installed on this computer:")
Console.WriteLine("")
' Iterate through the retrieved software products.
For Index = 0 To Key.SubKeyCount - 1
   ' Open the registry subkey that corresponds to the current software product.
   ' SubKeyNames(Index) contains the name of the node that corresponds to the 
   ' current software product.
   SubKey = Registry.LocalMachine.OpenSubKey _
      ("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" + "\" _
         + SubKeyNames(Index), False)
   ' Verify that the DisplayName exists. If the DisplayName does not exist, 
   ' return a null string. If the returned value is a null string, the 
   ' DisplayName does not exist, and the software product is not valid.
   If Not SubKey.GetValue("DisplayName", "") Is "" Then
      ' The current software product is valid.
      ' Display the DisplayName of this valid software product.
      Console.WriteLine(CType(SubKey.GetValue("DisplayName", ""), String))
   End If
Next

Complete code listing (Module1.vb)

Option Strict On

Imports Microsoft.Win32

Module Module1

    Sub Main()

        ' Open the Uninstall registry subkey.
        Dim Key As RegistryKey = Registry.LocalMachine.OpenSubKey _
           ("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", False)
        ' Retrieve a list of installed software products.
        ' This list also includes some software products that are not valid.
        Dim SubKeyNames() As String = Key.GetSubKeyNames()
        ' Declare a variable to iterate through the retrieved list of 
        ' installed software products.
        Dim Index As Integer
        ' Declare a variable to hold the registry subkey that correspond
        ' to each retrieved software product.
        Dim SubKey As RegistryKey
        Console.WriteLine("The following software products are installed on this computer:")
        Console.WriteLine("")
        ' Iterate through the retrieved software products.
        For Index = 0 To Key.SubKeyCount - 1
            ' Open the registry subkey that corresponds to the current software product.
            ' SubKeyNames(Index) contains the name of the node that corresponds to the 
            ' current software product.
            SubKey = Registry.LocalMachine.OpenSubKey _
               ("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" + "\" _
                  + SubKeyNames(Index), False)
            ' Verify that the DisplayName exists. If the DisplayName does not exist, 
            ' return a null string. If the returned value is a null string, the 
            ' DisplayName does not exist, and the software product is not valid.
            If Not SubKey.GetValue("DisplayName", "") Is "" Then
                ' The current software product is valid.
                ' Display the DisplayName of this valid software product.
                Console.WriteLine(CType(SubKey.GetValue("DisplayName", ""), String))
            End If
        Next

        Console.WriteLine("Press ENTER to quit.")
        Console.ReadLine()

    End Sub

End Module

Verify that the code works

  1. On the Build menu, click Build Solution.
  2. To run the application, click Start on the Debug menu. A Console window appears with a list of the installed software products.
  3. To quit the application, press ENTER.

Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbvs2005swept kbvs2005applies kbRegistry kbSample kbProgramming kbHOWTOmaster KB821775 kbAudDeveloper