If I run a process with ShellExecute
(or in .net with System.Diagnostics.Process.Start()
) the filename process to start doesn't need to be a full path.
If I want to start notepad, I can use
Process.Start("notepad.exe");
instead of
Process.Start(@"c:\windows\system32\notepad.exe");
because the directory c:\windows\system32
is part of the PATH environment variable.
how can I check if a file exists on the PATH without executing the process and without parsing the PATH variable?
System.IO.File.Exists("notepad.exe"); // returns false
(new System.IO.FileInfo("notepad.exe")).Exists; // returns false
but I need something like this:
System.IO.File.ExistsOnPath("notepad.exe"); // should return true
and
System.IO.File.GetFullPath("notepad.exe"); // (like unix which cmd) should return
// c:\windows\system32\notepad.exe
Is there a predefined class to do this task available in the BCL?
PATH
is platform-specific (e.g./
vs\
for subdirs, and:
vs;
for entry separators), as well as wanting to consistently handle ambiguous results - so it isn't as simple as "get the path then check exists()". Also, if there's dozens or paths to check, or if any are remote/network paths, then there might be significant performance implications of checking in program-code instead of the OS having a pre-cached result, for example. Etc. etc.