2010-05-14 39 views

回答

0

尝试搜索在CodePlex上一口流利的路径...它给快捷方式使用lambda表达式目录内检索算法对文件/ LINQ

0

看一看的DirectoryInfo类。

你可能会需要一点递归的事情

0

使用LINQ和Directory.EnumerateFiles

var files = 
    from file in Directory.EnumerateFiles(rootFolder,searchFor,SearchOption.AllDirectories) 
    select file; 
1

使用递归看看。

编写一个在特定文件夹中搜索文件的方法。 从每个子目录的本身内部调用该方法,并让它在找到该文件时返回该路径。

伪C#-Code(仅适用于获得的想法):

public string SearchFile (string path, string filename) 
{ 
    if (File.exists(path+filename)) return path; 

    foreach(subdir in path) 
    { 
     string dir = Searchfile(subdirpath,filename); 
     if (dir != "") return dir; 
    } 
} 

这将通过所有子目录运行和路径返回到搜索到的文件,如果它在那里,否则一个空字符串。

1

试试这个:

static string SearchFile(string folderPath, string fileToSearch) 
{ 
    string foundFilePath = null; 
    ///Get all directories in current directory 
    string[] directories = Directory.GetDirectories(folderPath); 

    if (directories != null && directories.Length > 0) 
    { 
     foreach (string dirPath in directories) 
     { 
      foundFilePath = SearchFile(dirPath, fileToSearch); 
      if (foundFilePath != null) 
      { 
       return foundFilePath; 
      } 
     }     
    } 

    string[] files = Directory.GetFiles(folderPath); 
    if (files != null && files.Length > 0) 
    { 
     foundFilePath = files.FirstOrDefault(filePath => Path.GetFileName(filePath).Equals(fileToSearch, StringComparison.OrdinalIgnoreCase));     
    } 

    return foundFilePath; 
}