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
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With