Click here to Skip to main content
Click here to Skip to main content

'which' utility for Windows

, 6 Jul 2005
Rate this:
Please Sign up or sign in to vote.
Find all possible locations of an executable.

Sample Image - WinWhich.gif

Introduction

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
 2 C:\WINDOWS\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)
{
  if(de.Key.ToString().ToUpper().Equals("PATH"))
  {
    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 && 
           this.additionalExtensionsString.Length>0)
{
  //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();
  al.AddRange(this.pathExt);
  al.AddRange(aes);
  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 += "$";
}
else
{
  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);
        }
    }
}

License

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

Share

About the Author

SathishVJ

France France
~/sathishvj

Comments and Discussions

 
GeneralOrder problem Pinmemberericksonbrian18-Oct-07 5:15 
GeneralQuoted paths Pinmembergewe12-Apr-06 3:23 
Generalbatch file PinmemberMCarter__17-Jul-05 17:04 
GeneralRe: batch file PinmemberSathishVJ20-Jul-05 22:21 
GeneralRe: batch file PinmemberSDX-{5A853460-2944-42f8-84B8-2432DE3657EF}5-Dec-06 20:55 
Generalwhere.exe Pinmemberschirrmeister15-Jul-05 21:38 
GeneralCOOL Pinmembereggie57-Jul-05 9:55 
GeneralRe: COOL PinmemberSathishVJ8-Jul-05 0:12 
Generalcomment and suggestion Pinmemberdfellman7-Jul-05 7:07 
GeneralRe: comment and suggestion PinmemberSathishVJ8-Jul-05 0:16 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140821.2 | Last Updated 7 Jul 2005
Article Copyright 2005 by SathishVJ
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid