Bug in Directories.GetFiles?

  • Thread starter Thread starter Claire
  • Start date Start date
C

Claire

The following is a self calling function to search directories for an
instance of a particular executable. VS .net 2003.
On one pass through, Im getting an error DirectoryNotFoundException when the
Directory.GetFiles(dir, "pocketvibrapro.exe") line is called.
At that stage the dir is "\Program Files\McmCollector ". This is the
directory from where my active application is being debugged.
I've also tried using dir.trim in case the final space char caused a
problem.
Any ideas?

public static string SearchDirectory(string dir)
{
string str2;
// Query for exe
string[] str = Directory.GetFiles(dir, "pocketvibrapro.exe");
// If found return directory
if (str.Length > 0)
return dir;
// Read subdirectories
str = Directory.GetDirectories(dir);
// search subdirectories
for (int nCount = 0; nCount < str.Length; nCount++)
{
str2 = SearchDirectory(str[nCount]);
if (str2 != "")
return str[nCount];
}
return "";
}
 
How was that dir value generated? does the directory really have a trailing
space? What happens if you add a backslash e.g.
dir = \\Program Files\\McmCollector \\;

To search for a specific file use File.Exists e.g.

if(File.Exists(Path.Combine(dir, "pocketvibrapro.exe")))
{
//do something with dir
}

Peter
 
Hi Peter,
The following code (copy directly from my app) fails on the directory that
the program itself runs from and no other: The dir parameter is of the same
structure for all other calls to the function but they pass.
eg dir would enter containing the value "\program files\my prog "

private void cmdStart_Click(object sender, System.EventArgs e)
{
// Look from root for exe file
if (Globals.FindVibra("\\") == "")
{foo;}
}

// Self calling function
public static string FindVibra(string dir)
{
string fnName = "FindVibra";
string str2;
try
{
// Query for exe
string[] str = Directory.GetFiles(dir, "pocketvibrapro.exe");
// If found return directory
if (str.Length > 0)
return dir;
// Read subdirectories
str = Directory.GetDirectories(dir);
// search subdirectories
for (int nCount = 0; nCount < str.Length; nCount++)
{
str2 = FindVibra(str[nCount]);
if (str2 != "")
return str2;
}
}
catch(Exception e)
{
LogMessage(fnName, "Exception e: " + e.Message);
}
return "";
}
 
Back
Top