Arunachalam
Arunachalam

Reputation: 6097

how to find the execution path of a installed software

How can i find the execution path of a installed software in c# for eg media player ,vlc player . i just need to find their execution path . if i have a vlc player installed in my D drive . how do i find the path of the VLC.exe from my c# coding

Upvotes: 11

Views: 27226

Answers (6)

jerryurenaa
jerryurenaa

Reputation: 4712

None of the answers worked for me. After hours of searching online, I was able to successfully get the installation path. Here is the final code.

public static string checkInstalled(string findByName)
    {
        string displayName;
        string InstallPath;
        string registryKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";

        //64 bits computer
        RegistryKey key64 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
        RegistryKey key = key64.OpenSubKey(registryKey);

        if (key != null)
        {
            foreach (RegistryKey subkey in key.GetSubKeyNames().Select(keyName => key.OpenSubKey(keyName)))
            {
                displayName = subkey.GetValue("DisplayName") as string;
                if (displayName != null && displayName.Contains(findByName))
                {

                    InstallPath = subkey.GetValue("InstallLocation").ToString();

                    return InstallPath; //or displayName

                }
            }
            key.Close();
        }

        return null;
    }

you can call this method like this

string JavaPath = Software.checkInstalled("Java(TM) SE Development Kit");

and boom. Cheers

Upvotes: 3

Fredrik Mörk
Fredrik Mörk

Reputation: 158389

Using C# code you can find the path for some excutables this way:

private const string keyBase = @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths";
private string GetPathForExe(string fileName)
{
    RegistryKey localMachine = Registry.LocalMachine;
    RegistryKey fileKey = localMachine.OpenSubKey(string.Format(@"{0}\{1}", keyBase, fileName));
    object result = null;
    if (fileKey != null)
    {
        result = fileKey.GetValue(string.Empty);
        fileKey.Close();
    }


    return (string)result;
}

Use it like so:

string pathToExe = GetPathForExe("wmplayer.exe");

However, it may very well be that the application that you want does not have an App Paths key.

Upvotes: 19

Country Don
Country Don

Reputation: 61

I used the CurrentVersion\Installer\Folders registry key. Just pass in the product name.

private string GetAppPath(string productName)
    {
        const string foldersPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\Folders";
        var baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);

        var subKey = baseKey.OpenSubKey(foldersPath);
        if (subKey == null)
        {
            baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
            subKey = baseKey.OpenSubKey(foldersPath);
        }
        return subKey != null ? subKey.GetValueNames().FirstOrDefault(kv => kv.Contains(productName)) : "ERROR";          
    }

Upvotes: 6

RobV
RobV

Reputation: 28675

This method works for any executable located in a folder which is defined in the windows PATH variable:

private string LocateEXE(String filename)
{
    String path = Environment.GetEnvironmentVariable("path");
    String[] folders = path.Split(';');
    foreach (String folder in folders)
    {
        if (File.Exists(folder + filename))
        {
            return folder + filename;
        } 
        else if (File.Exists(folder + "\\" + filename)) 
        {
            return folder + "\\" + filename;
        }
    }

    return String.Empty;
}

Then use it as follows:

string pathToExe = LocateEXE("example.exe");

Like Fredrik's method it only finds paths for some executables

Upvotes: 7

Kevin Newman
Kevin Newman

Reputation: 2447

This stackoverflow.com article describes how to get the application associated with a particular file extension.

Perhaps you could use this technique to get the application associated with certain extensions, such as avi or wmv - either Medial Player or in your case VLC player?

Upvotes: 0

joshcomley
joshcomley

Reputation: 28838

Have a look at MsiEnumProductsEx

Upvotes: 0

Related Questions