How to enumerate installed MSI Products and their MSP Patches using C#

Hey all,

I already have a C++ example of doing this, but that isn’t much help when you have to do it in C#. So it is good to know how to do this in both languages.

Here is what I did:

  1. Created a new C# Console Application project in Visual Studio and named it WindowsInstallerTest.
  2. I added a reference to Microsoft.Deployment.WindowsInstaller. I found this by installing Windows Installer XMl and looking here:
    C:\Program Files (x86)\Windows Installer XML v3.6\bin\Microsoft.Deployment.WindowsInstaller.dll
  3. I added a using statement to the same Microsoft.Deployment.WindowsInstaller.
  4. I enumerated the installed MSI products and then with a foreach loop output data on each.
  5. I enumerated the MSP patches for each product and used another foreach loop to output data on each product.

Here is the simple code.  Hopefully this is enough to get your started with writing code to work with installed MSI products.

using System;
using System.Collections.Generic;
using System.Linq;
// 
// Step 1 - Add a reference to Microsoft.Deployment.WindowsInstaller
//          and then add this using statement.
using Microsoft.Deployment.WindowsInstaller;

namespace WindowsInstallerTest
{
    class Program
    {
        static void Main(string[] args)
        {
            // Step 2 - Get the installed MSI Products
            IEnumerable<ProductInstallation> installations = Microsoft.Deployment.WindowsInstaller.ProductInstallation.GetProducts(null, "s-1-1-0", UserContexts.All);
            int i = 0;

            // Step 3 - Loop through the installed MSI Products and output information

            foreach (ProductInstallation installation in installations)
            {
                Console.WriteLine("Id: " + ++i);
                Console.WriteLine("Name: " + installation.ProductName);
                Console.WriteLine("ProductVersion: " + installation.ProductVersion);
                Console.WriteLine("Install Source: " + installation.InstallSource);
                Console.WriteLine("Patches: ");

                // Step 4 - Get the installed MSP Patches for the current installation
                IEnumerable<PatchInstallation> patches = PatchInstallation.GetPatches(null, installation.ProductCode, "s-1-1-0", UserContexts.All, PatchStates.All);

                // Step 5 - Loop through the installed MSP Patches and output information
                int j = 0;
                foreach (PatchInstallation patch in patches)
                {
                    Console.WriteLine("  " + ++j + ": " + patch.DisplayName);
                    Console.WriteLine("  Cache: " + patch.LocalPackage);
                }
                Console.WriteLine();
            }
        }
    }
}

3 Comments

  1. Dominic says:

    Just wanted to say thanks. I wasn't aware of the stuff in the wix .dll. Its so going to simplify my code. I normally open the msi/msp and query it directly to get product codes and all other info. Much easier with this code! I get almost everything in one place.

    Thanks!

  2. Ty says:

    Any ideas why I would get an exception when I run this code where it says "Access is denied"? ('Microsoft.Deployment.WindowsInstaller.InstallerException')

Leave a Reply to Michael

How to post code in comments?