
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)
{
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);
}
}
}