2010-10-22 79 views
17

在C#中,如何检查目录或其子目录中是否存在特定文件?如何检查目录或其任何子目录中是否存在特定文件

System.IO.File.Exists似乎只接受一个没有重载搜索子目录的参数。

我可以使用SearchOption.AllDirectories超载与LINQ和System.IO.Directory.GetFiles做到这一点,但似乎有点重手。

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

回答

33

如果你正在寻找一个特定的文件名,使用*.*确实重手。试试这个:

var file = Directory.GetFiles(tempScanStorage, foo, SearchOption.AllDirectories) 
        .FirstOrDefault(); 
if (file == null) 
{ 
    // Handle the file not being found 
} 
else 
{ 
    // The file variable has the *first* occurrence of that filename 
} 

注意,这是不太你当前的查询做什么 - 因为你当前的查询会发现“xbary.txt”如果你富只是bar。我不知道这是否是有意的。

如果你想知道多个匹配,你当然不应该使用FirstOrDefault()。目前尚不清楚你想要做什么,这使得很难给出更具体的建议。

请注意,在.NET 4中也有Directory.EnumerateFiles,它可能会或可能不会为您执行更好。我非常怀疑,当你搜索一个特定的文件(而不是目录和子目录中的所有文件)时它会有所作为,但至少值得了解。编辑:正如评论中指出的那样,它can make a difference if you don't have permission to see all the files in a directory

+0

是的,我希望找到一个特定的文件。我更新了'*。*'。感谢严密的解决方案。 – Dhaust 2010-10-24 22:58:23

+3

如果你正在扫描你没有任何权限的目录(例如,在有RecycleBin的驱动器级别),EnumerateFiles确实有所帮助。 参考http://stackoverflow.com/questions/1393178解决方案,如果你遇到这种情况。 – Malcolm 2012-07-25 22:36:55

+0

@Malcolm:谢谢;编辑成答案。 – 2012-07-26 05:53:53

5

另一种方法是你自己写的搜索功能,其中之一应该工作:

private bool FileExists(string rootpath, string filename) 
    { 
     if(File.Exists(Path.Combine(rootpath, filename))) 
      return true; 

     foreach(string subDir in Directory.GetDirectories(rootpath, "*", SearchOption.AllDirectories)) 
     { 
      if(File.Exists(Path.Combine(subDir, filename))) 
      return true; 
     } 

     return false; 
    } 

    private bool FileExistsRecursive(string rootPath, string filename) 
    { 
     if(File.Exists(Path.Combine(rootPath, filename))) 
      return true; 

     foreach (string subDir in Directory.GetDirectories(rootPath)) 
     { 
      return FileExistsRecursive(subDir, filename); 
     } 

     return false; 
    } 

第一方法仍然提取所有的目录名称,当有许多子目录但文件接近顶端时会更慢。

第二个是递归,在'最糟糕的情况'情况下会比较慢,但当有许多嵌套的子级时,会更快,但是文件位于顶级目录中。

0

这是一个递归搜索函数,一旦找到您指定的文件就会突然出现。请注意参数应指定为fileName(例如testdb.bak)和目录(例如c:\ test)。

请注意,如果您在具有大量子目录和文件的目录中执行此操作,速度可能会非常慢。

private static bool CheckIfFileExists(string fileName, string directory) {    
     var exists = false; 
     var fileNameToCheck = Path.Combine(directory, fileName); 
     if (Directory.Exists(directory)) { 
      //check directory for file 
      exists = Directory.GetFiles(directory).Any(x => x.Equals(fileNameToCheck, StringComparison.OrdinalIgnoreCase)); 

      //check subdirectories for file 
      if (!exists) { 
       foreach (var dir in Directory.GetDirectories(directory)) { 
        exists = CheckIfFileExists(fileName, dir);        

        if (exists) break; 
       } 
      } 
     } 
     return exists; 
    } 
1

检查现有的任何特定目录下的文件做以下 注:“UPLOADEDFILES”是文件夹的名称。

File.Exists(使用Server.Mappath( “UPLOADEDFILES /”))

享受编码

相关问题