Get path to executable from command (as cmd does)

Viewed 9956

Given a command-line style path to a command such as bin/server.exe or ping, how can I get the full path to this executable (as cmd or Process.Start would resolve it)?

I tried Path.GetFullPath, but it always expands relative to the working directory. It expands bin/server.exe correctly, however given ping it returns c:\users\matt\ping (non-existent). I want c:\Windows\system32\ping.exe.

Edit: I would like the same behaviour as cmd. Some considerations:

  1. When there is a local executable with the same name as one in the path, cmd prefers the local one
  2. cmd can expand the command server to server.bat or server.exe (adding the file extension)

I also tried Windows' command-line tool called where . It does almost I want:

Displays the location of files that match the search pattern. By default, the search is done along the current directory and in the paths specified by the PATH environment variable.

>where ping
C:\Windows\System32\PING.EXE
>where bin\server
INFO: Could not find files for the given pattern(s).

(This question is hard to search around because of the two different meanings of the word 'path')

5 Answers
Related