Alex
Alex

Reputation: 7622

How can I get another application's installation path programmatically?

I'd like to know where the installation path for an application is. I know it usually is in ...\Program Files... but I guess some people install it in different locations. I do know the name of the application.

Thank you.

Upvotes: 9

Views: 19745

Answers (6)

komodosp
komodosp

Reputation: 3658

Take a look in the registry.

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\

or

HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\

Each of the above contain a list of sub-keys, one for each installed application (as it appears, for example, in the "Programs and Features" applet)

You can search for your application there, or if you know the product code, access it directly.

    public string GetInstallPath(string applicationName)
    {
        var installPath = FindApplicationPath(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", applicationName);

        if (installPath == null)
        {
            installPath = FindApplicationPath(@"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall", applicationName);
        }

        return installPath;
    }

    private string FindApplicationPath(string keyPath, string applicationName)
    {

        var hklm = Registry.LocalMachine;
        var uninstall = hklm.OpenSubKey(keyPath);
        foreach (var productSubKey in uninstall.GetSubKeyNames())
        {
            var product = uninstall.OpenSubKey(productSubKey);

            var displayName = product.GetValue("DisplayName");
            if (displayName != null && displayName.ToString() == applicationName)
            {
                return product.GetValue("InstallLocation").ToString();
            }

        }

        return null;
    }

Upvotes: 0

Ali Alavi
Ali Alavi

Reputation: 2467

You can use MSI (I wrote a C# wrapper for it here https://github.com/alialavia/MSINet). Here is a simple example:

var location = "";
foreach (var p in InstalledProduct.Enumerate())
{
    try
    {
        if (p.InstalledProductName.Contains("AppName"))                     
        {
            location = p.InstallLocation;
            break;
        }
    } 
    catch { }
}

Upvotes: 0

Sandeep Singh Rawat
Sandeep Singh Rawat

Reputation: 1647

Best way is to use Installer APIs to find the program location. You can write a Managed wrapper over the APIs

Search for MsiGetProductInfo

Reference: http://msdn.microsoft.com/en-us/library/aa369558(VS.85).aspx

Upvotes: 3

Gern Blanston
Gern Blanston

Reputation: 42660

If you know the application in question (as compared to any application) registry key is the probably the best option (if one exists).

The install might put in its own custom "install path key" somewhere (so do a find as Fara mentioned) or it might be in the uninstall section for installed programs, so you could check:

  • HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall

But be aware that any new version of an install could change the key it writes out, both for a custom key or for the uninstall entry. So checking the registry should probably be only for a known install\version.

tep

Upvotes: 4

Andreas Rejbrand
Andreas Rejbrand

Reputation: 109013

Many (most?) programs create an App Paths registry key. Have a look at

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths

Upvotes: 12

Alex McBride
Alex McBride

Reputation: 7021

The ideal way to find a program's installation path (on Windows) is to read it from the registry. Most installers will create a registry key for that program that contains the installation path. Exactly where this key is and what it will be named varies depending on the program in question.

To find if the program has a key in the registry, open 'regedit' and use the Edit > Find option to try and locate a key with the program name. If such a key exists, you can read it using the RegistryKey class in the .NET Framework library.

If the program does not have a registry key then another option is just to ask the user to locate the .exe file with the OpenFileDialog, although this is obviously not ideal.

Upvotes: 13

Related Questions