How to check if a specific file exists in directory or any of its subdirectories

Viewed 85362

In C#, how do I check if a specific file exists in a directory or any of its subdirectories?

System.IO.File.Exists only seems to accept a single parameter with no overloads to search subdirectories.

I can do it with LINQ and System.IO.Directory.GetFiles using the SearchOption.AllDirectories overload, but that seems a bit heavy handed.

var MyList = from f in Directory.GetFiles(tempScanStorage, "foo.txt", SearchOption.AllDirectories)
             where System.IO.Path.GetFileName(f).ToUpper().Contains(foo)
             select f;

foreach (var x in MyList)
{
    returnVal = x.ToString();
}  
5 Answers
Related