1

I need to get the path to an executable, and the information that I got is only the executable name. I need the file path, not the .exe name

Example : I have "arma3.exe" only, now I need to find the path to it. I need something like this "C:\Program Files\Arma3\arma3.exe" <-- the file path

I am NOT asking for "How do I find the path to my CURRENT WPF APPLICATION executable path" nor "how do I get the file name given X path".

And please, don't ask me why I need to find the path or bla3. I have my own reason to do this.

user2864740
  • 60,010
  • 15
  • 145
  • 220
Xeon
  • 246
  • 3
  • 15
  • Note that there could be multiple executables of the same name in different locations on your machine. So lacking other information, the file name alone might not uniquely identify an executable. – Tim Destan May 21 '14 at 02:10

2 Answers2

0

What you are asking is basically a search for the file name... You can do this in many ways, like:

Don't forget that there may be more than one disk drive:

In the examples cited above, the return of methods in the search contains the folder name in variable, so you can just write:

IEnumerable<string> matchFiles = possibleFiles.Where(a => a.EndsWith("/fileName.exe"))

Another care that you must have is with the folder permissions, be alert to possible exceptions..

Keep in mind that this search can take a while, maybe there's other ways to search, as in the windows registry, or only in indexed files, or using more than one thread, but that would be an answer to another question.

Community
  • 1
  • 1
J. Lennon
  • 3,311
  • 4
  • 33
  • 64
0

If you're only looking for executables that are most likely in the Program Files directory, you can the GetFiles method of the System.IO.Directory class:

string NewPath = System.IO.Directory.GetFiles(@"C:\Program Files\", "arma3.exe").FirstOrDefault();

If there could be more than one file:

string[] NewPaths = System.IO.Directory.GetFiles(@"C:\Program Files\", "arma3.exe");
tinstaafl
  • 6,908
  • 2
  • 15
  • 22