Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get path to executable from command (as cmd does)

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')

like image 447
Colonel Panic Avatar asked Jul 26 '12 11:07

Colonel Panic


People also ask

How can you know the path of an executable file command?

PATH without parameters will display the current path. The %PATH% environment variable contains a list of folders. When a command is issued at the CMD prompt, the operating system will first look for an executable file in the current folder, if not found it will scan %PATH% to find it.

How do you go to a file path in cmd?

Navigate to the location of your file by typing the following into the command prompt window: Users\”Username”> cd C:\”Users\”Username”\”Location” In this example, the “Username” will be User and the “Location” will be desktop. Then type in the name and extension of the file you're trying to open: “Filename.

Can you run an EXE from cmd?

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.


2 Answers

Considering PATHEXT too, stealing from Serj-Tm's answer (sorry! +1 to him):

public static string WhereSearch(string filename)
{
    var paths = new[]{ Environment.CurrentDirectory }
            .Concat(Environment.GetEnvironmentVariable("PATH").Split(';'));
    var extensions = new[]{ String.Empty }
            .Concat(Environment.GetEnvironmentVariable("PATHEXT").Split(';')
                       .Where(e => e.StartsWith(".")));
    var combinations = paths.SelectMany(x => extensions,
            (path, extension) => Path.Combine(path, filename + extension));
    return combinations.FirstOrDefault(File.Exists);
}

Sorry the indentation's a bit all-over-the-place - I was trying to make it not scroll. I don't know if the StartsWith check is really necessary - I'm not sure how CMD copes with pathext entries without a leading dot.

like image 78
Rup Avatar answered Sep 20 '22 05:09

Rup


public static string GetFullPath(string filename)    
{
 return new[]{Environment.CurrentDirectory}
  .Concat(Environment.GetEnvironmentVariable("PATH").Split(';'))
  .Select(dir => Path.Combine(dir, filename))
  .FirstOrDefault(path => File.Exists(path));
}
like image 30
Serj-Tm Avatar answered Sep 23 '22 05:09

Serj-Tm