8

Is there any way to find a path in C# dynamically without executing "where" command prompt command?

For example, if I want to find mspaint exe, I can type this in command prompt

where mspaint

and it returns the path.

AccurateEstimate
  • 353
  • 2
  • 14
  • 2
    You should do a filesystem search based on your path. That's all that where does anyways. Just to prove the point: http://stackoverflow.com/a/304447/310196 – devshorts Mar 05 '14 at 22:29
  • Possible duplicate of http://stackoverflow.com/questions/5565972/how-i-can-execute-cmd-command-in-c-sharp-console-application?lq=1 – grovesNL Mar 05 '14 at 22:30
  • 1
    Not sure how thats relevant @grovesNL. The question wants to implement the "where" command in c# and not call any external process. – devshorts Mar 05 '14 at 22:31
  • possible duplicate of [Is there a way to find a file by just its name in C#?](http://stackoverflow.com/questions/3754993/is-there-a-way-to-find-a-file-by-just-its-name-in-c) – Hans Passant Mar 05 '14 at 22:33
  • @devshorts It's debatable whether the phrasing disallowed launching and capturing the task within C#. – grovesNL Mar 05 '14 at 22:38

2 Answers2

7

I don't think there is a built-in method in the Common Language Runtime to do this for you, but you can certainly do it yourself:

  • Get the value of the PATH environment variable
  • Split it on ; delimiters to get a list of directories in the path
  • Check each of those directories to see if it contains program

Example:

public static string FindInPath(string filename)
{
    var path = Environment.GetEnvironmentVariable("PATH");
    var directories = path.Split(';');

    foreach (var dir in directories)
    {
        var fullpath = Path.Combine(dir, filename);
        if (File.Exists(fullpath)) return fullpath;
    }

    // filename does not exist in path
    return null;
}

Don't forget to add .exe to the filename. (Or, you could modify the code above to search for any executable extension: .bat, .com, .exe; or perhaps even any extension at all.)

TypeIA
  • 15,869
  • 1
  • 32
  • 47
  • Works like a charm but slower than where. Thanks a lot! – AccurateEstimate Mar 05 '14 at 23:04
  • I'm surprised to hear it's slower. How did you measure the times? Perhaps `where` uses some kind of cache, but that seems surprising. – TypeIA Mar 05 '14 at 23:57
  • It might also be worth first checking in `Environment.CurrentDirectory`. Not sure if there are other folders that should be included, but it would be worth doing some more research. Perhaps the current process assembly's path should also be searched or some system directories. – Filip Skakun Mar 20 '17 at 04:21
  • Where documentation says: _"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."_ – Filip Skakun Mar 20 '17 at 04:31
  • 1
    The ["whereis" article](https://www.codeproject.com/kb/cs/whereis.aspx?target=whereis) on CodeProject is a more complete implementation and also searches for the paths I mentioned. – Filip Skakun Mar 20 '17 at 04:33
  • you should use `PATHEXT` to get the extensions of the files to execute. – John Gietzen Jul 21 '20 at 18:57
0

This is based on @TypeIA's answer, but it supports the current directory and all PATHEXT.

public static string Where(string file)
{
    var paths = Environment.GetEnvironmentVariable("PATH").Split(';');
    var extensions = Environment.GetEnvironmentVariable("PATHEXT").Split(';');
    return (from p in new[] { Environment.CurrentDirectory }.Concat(paths)
            from e in new[] { string.Empty }.Concat(extensions)
            let path = Path.Combine(p.Trim(), file + e.ToLower())
            where File.Exists(path)
            select path).FirstOrDefault();
}
John Gietzen
  • 45,925
  • 29
  • 140
  • 183