Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to find the execution path of a installed software

Tags:

c#

windows

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

like image 475
Arunachalam Avatar asked May 26 '09 10:05

Arunachalam


People also ask

How do I find my program path?

To find the installation folder of a program using a desktop shortcut: From your desktop, right-click on the program's shortcut. Click on the Properties, and the Properties window should now be displayed. Click on the Shortcut tab, and you will find the installation path in the Target field.

How do I find the path of a software in Linux?

You can find the directory where a program is installed with the whereis or which command. If you want to know a program's full pathname, you can use the whereis program. The whereis program looks in a predefined set of directories for the named program. This tells you that the who program is in /usr/bin.

How do I find a program using command prompt?

Type "start [filename.exe]" into Command Prompt, replacing "filename" with the name of your selected file. Replace "[filename.exe]" with your program's name. This allows you to run your program from the file path.


2 Answers

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.

like image 58
Fredrik Mörk Avatar answered Sep 24 '22 03:09

Fredrik Mörk


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

like image 39
RobV Avatar answered Sep 21 '22 03:09

RobV