Click here to Skip to main content
14,355,411 members

'which' utility for Windows

Rate this:
3.30 (5 votes)
Please Sign up or sign in to vote.
3.30 (5 votes)
6 Jul 2005
Find all possible locations of an executable.

Sample Image - WinWhich.gif


This simple program lets you find out the location of the executable that is being run. This was useful when running the 'java' executable and I was not sure which version of the JRE was actually being run.

Source code and Demo

The demo can be tested to find out the version of Notepad that is executed like this:

c:\temp\winwhich notepad
 1 C:\WINDOWS\system32\notepad.exe

This says that two versions of the executable Notepad were found and the version that will be run first is the executable under system32.

Code Walkthrough

Initially we examine the options provided for the program and assign them to internal variables. The core of the program is the method Search which takes the executable name as a parameter.

public void Search(string executableName)

Within Search, first we find the value of the 'PATH' environment variable. The various directories in this variable are where the system will search for the mentioned program. We also find the registered executable extensions by examining the PATHEXT environment variable.

IDictionary  environmentVariables = Environment.GetEnvironmentVariables();
foreach (DictionaryEntry de in environmentVariables)
    pathString = de.Value.ToString().Trim();
  if (de.Key.ToString().ToUpper().Equals("PATHEXT"))
    pathExt = de.Value.ToString().Trim().Split(new char[]{';'});

If there are additional extensions provided, we append it to the pathExt variable.

if (this.additionalExtensionsString != null && 
  //a simple precaution to remove the last semicolon if there is one
  if (this.additionalExtensionsString.EndsWith(";"))
     this.additionalExtensionsString = this.additionalExtensionsString.Remove(
                     this.additionalExtensionsString.Length-1, 1);
  string[] aes = this.additionalExtensionsString.Split(new char[]{';'});
  ArrayList al = new ArrayList();
  this.pathExt = (string[])al.ToArray(typeof(string));

We then create a regular expression that will match the name of the executable.

Regex regEx = new Regex(GetRegExString(executableName), RegexOptions.IgnoreCase);

Within GetRegExString, we check if the extension has already been provided or not. If it has not been, we add the probable extensions.

if (execNameHasExtension || this.pathExt == null || this.pathExt.Length == 0)
   regexString += "$";
  regexString += "(?:";
  foreach(string oneExt in this.pathExt)
    regexString += "\\"+oneExt+"|";
  regexString += ")$";

GetPathsSplit function returns an array of all the components of the PATH. Within this, we also add the start up directory (".") to the list of paths, since Windows examines this first. Then we walk through each of the paths looking for files that match, writing out matches as we find them.

string[] paths = pathString.Split(new char[]{Path.PathSeparator});
int count = 0;
foreach(string onePath in paths)
    string onePath2 = onePath.Trim();
    if (onePath2.Length == 0) continue;
    DirectoryInfo di = new DirectoryInfo(onePath2);
    if (!di.Exists) continue;
    foreach(FileInfo fi in di.GetFiles())
        if (regEx.IsMatch(fi.Name))
            Console.WriteLine(" "+ ++count + " " +fi.FullName);


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

France France

Comments and Discussions

GeneralOrder problem Pin
ericksonbrian18-Oct-07 6:15
memberericksonbrian18-Oct-07 6:15 
GeneralQuoted paths Pin
gewe12-Apr-06 4:23
membergewe12-Apr-06 4:23 
Generalbatch file Pin
MCarter__17-Jul-05 18:04
memberMCarter__17-Jul-05 18:04 
GeneralRe: batch file Pin
SathishVJ20-Jul-05 23:21
memberSathishVJ20-Jul-05 23:21 
GeneralRe: batch file Pin
Sandeep Datta5-Dec-06 21:55
memberSandeep Datta5-Dec-06 21:55 
Generalwhere.exe Pin
schirrmeister15-Jul-05 22:38
memberschirrmeister15-Jul-05 22:38 
GeneralCOOL Pin
eggie57-Jul-05 10:55
membereggie57-Jul-05 10:55 
GeneralRe: COOL Pin
SathishVJ8-Jul-05 1:12
memberSathishVJ8-Jul-05 1:12 
Generalcomment and suggestion Pin
dfellman7-Jul-05 8:07
memberdfellman7-Jul-05 8:07 
GeneralRe: comment and suggestion Pin
SathishVJ8-Jul-05 1:16
memberSathishVJ8-Jul-05 1:16 
QuestionGUI? Pin
fwsouthern7-Jul-05 4:59
memberfwsouthern7-Jul-05 4:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 6 Jul 2005


21 bookmarked