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
can u please explain me in detail .
Arunachalam
2009-05-26 10:12:56
+1
A:
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.
Fredrik Mörk
2009-05-26 10:19:53
A:
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
RobV
2009-05-26 10:47:30
small corrections so that your code will work: 1)All part of ur code doesnt return a value,2)'LocateEXE'(misspelled)3)folder + "\\" + filename.But this will work only when the Path Variables are Set by the application that u install. In most cases it wont work. And what is the else if part doing.? Can u explain that.?
pragadheesh
2009-05-27 05:02:37
RobV
2009-05-28 09:31:52
yes its true that it won't work for a lot of cases, but that's the same as Fredrik's suggestion and we both pointed out that it won't work all the time.It may be easier just to have an AppSetting or similar in your app which you ask the end user to configure to point to VLC
RobV
2009-05-28 09:34:12
A:
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?
Bing
2009-05-26 12:56:56