Microsoft KB Archive/821775

= How to enumerate the software products that can be uninstalled on a computer =

Article ID: 821775

Article Last Modified on 5/16/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



IN THIS TASK

 * SUMMARY
 * Requirements
 * Create a Console Application
 * Open the Uninstall Registry Subkey
 * Retrieve a List of Installed Software Products
 * Get Rid of Software Products That Are Not Valid
 * Complete Code Listing (Module1.vb)
 * Verify That the Code Works
 * REFERENCES

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:

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
 Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. On the File menu, point to New, and then click Project. Under Project Types, click Visual Basic Projects.

Note In Visual Studio 2005, click Visual Basic under Project Types. Under Templates, click Console Application. In the Name box, type SoftwareDemo, and then click OK. By default, Module1.vb is created.  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:

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 _ (&quot;SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall&quot;, 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 &quot;Open the uninstall registry subkey&quot; 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 &quot;Retrieve a list of installed software products&quot; 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(&quot;The following software products are installed on this computer:&quot;) Console.WriteLine(&quot;&quot;) ' 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 _ (&quot;SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall&quot; + &quot;\&quot; _        + 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(&quot;DisplayName&quot;, &quot;&quot;) Is &quot;&quot; Then ' The current software product is valid. ' Display the DisplayName of this valid software product. Console.WriteLine(CType(SubKey.GetValue(&quot;DisplayName&quot;, &quot;&quot;), 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 _ (&quot;SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall&quot;, 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(&quot;The following software products are installed on this computer:&quot;) Console.WriteLine(&quot;&quot;) ' 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 _ (&quot;SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall&quot; + &quot;\&quot; _                 + 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(&quot;DisplayName&quot;, &quot;&quot;) Is &quot;&quot; Then ' The current software product is valid. ' Display the DisplayName of this valid software product. Console.WriteLine(CType(SubKey.GetValue(&quot;DisplayName&quot;, &quot;&quot;), String)) End If       Next

Console.WriteLine(&quot;Press ENTER to quit.&quot;) 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.

<div class="references_section">