views:

260

answers:

3

I have an application that is used on several hundred computers across the company that I must modify an INI file in the installation directory of the application. Users can install the application where ever they wish, and can have multiple versions of the application installed at any given time. I need to be able to find that installation directory.

Methods I've considered so far:

  • Using the WindowsInstaller to find the product by name and find its installation directory. (from here). --This almost worked, but the properties I'd expect to be populated (TARGETDIR, APPDIR) aren't.
  • Looking through the registry to find the installation directory for the particular app. It's not in there.
  • MsiGetComponentPath()? I saw this in the same link mentioned above, but I don't know how to implement it. I can get the ProductID using windows installer, but I don't know how to programmatically just choose a component and find its ID at random. Anyone?

Alright, lets hear any other methods for programmatically determining the installation directory of a Windows application.

A: 

If the install is an MSI then getting the information from WMI is trivial. The Win32_Product class has an InstallLocation property to hold this information.

EBGreen
A: 

Using WMI could work for some people, unfortunately our users won't have credentials allowing them to do this on their machines:

        ManagementObjectSearcher search = new ManagementObjectSearcher("Select InstallationLocation from Win32_Product");
        ManagementObjectCollection results = search.Get();

        foreach (ManagementObject mo in results)
        {
            Console.WriteLine(mo["InstallLocation"]);
        }
snicker
A: 

Well I came up with a solution that worked for me:

        Type type = Type.GetTypeFromProgID("WindowsInstaller.Installer");
        Installer msi = (Installer)Activator.CreateInstance(type);
        foreach (string productcode in msi.Products)
        {
            string productname = msi.get_ProductInfo(productcode, "InstalledProductName");
            if (productname.Contains("<APPLICATION NAME>"))
            {
                string installdir = msi.get_ProductInfo(productcode, "InstallLocation");
            }
        }
snicker