tags:

views:

56

answers:

3

How do I get a list of software products which are installed on the system. My goal is to iterate through these, and get the installation path of a few of them.

PSEUDOCODE ( combining multiple languages :) )

foreach InstalledSoftwareProduct
    if InstalledSoftwareProduct.DisplayName LIKE *Visual Studio*
        print InstalledSoftwareProduct.Path

Thanks

A: 

Well if all the programs you need store their install paths on the registry you can use something like: http://visualbasic.about.com/od/quicktips/qt/regprogpath.htm (I know it's VB but same principle).

I'm sure there probably is a way to get the Program list via .NET if some don't store their install paths (or do it obscurely), but I don't know it.

Blam
+2  A: 

You can ask the WMI Installed applications classes: the Win32_Products class represents all products installed by Windows Installer. For instance the following PS script will retrieve all prodcuts installed on local computer that were installed by Windows Installer:

Get-WmiObject -Class Win32_Product -ComputerName .

See Working with Software Installations. POrting the PS query to the equivalent C# use of WMI API (in other words Using WMI with the .NET Framework) is left as an exercise to the reader.

Remus Rusanu
Awesome Powershell command! I had fun writing a script that would compare what is installed on computers A and B but not C and D (Something worked on the first group, but not the second :) ).
Hamish Grubijan
+1  A: 

You can use MSI api functions to enumerate all installed products. Below you will find sample code which does that.

In my code I first enumerate all products, get the product name and if it contains the string "Visual Studio" I check for the InstallLocation property. However, this property is not always set. I don't know for certain whether this is not the right property to check for or whether there is another property that always contains the target directory. Maybe the information retrieved from the InstallLocation property is sufficient for you?

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;

class Program
{
    [DllImport("msi.dll", CharSet = CharSet.Unicode)]
    static extern Int32 MsiGetProductInfo(string product, string property,
        [Out] StringBuilder valueBuf, ref Int32 len);

    [DllImport("msi.dll", SetLastError = true)]
    static extern int MsiEnumProducts(int iProductIndex, 
        StringBuilder lpProductBuf);

    static void Main(string[] args)
    {
        StringBuilder sbProductCode = new StringBuilder(39);
        int iIdx = 0;
        while (
            0 == MsiEnumProducts(iIdx++, sbProductCode))
        {
            Int32 productNameLen = 512;
            StringBuilder sbProductName = new StringBuilder(productNameLen);

            MsiGetProductInfo(sbProductCode.ToString(),
                "ProductName", sbProductName, ref productNameLen);

            if (sbProductName.ToString().Contains("Visual Studio"))
            {
                Int32 installDirLen = 1024;
                StringBuilder sbInstallDir = new StringBuilder(installDirLen);

                MsiGetProductInfo(sbProductCode.ToString(),
                    "InstallLocation", sbInstallDir, ref installDirLen);

                Console.WriteLine("ProductName {0}: {1}", 
                    sbProductName, sbInstallDir);
            }
        }
    }
}
0xA3