如何搜索嵌套文件夹内的文件?查找嵌套文件夹内的文件
1
A
回答
3
这个函数
System.IO.DirectoryInfo.GetFiles("SearchPattern",SearchOption.AllDirectories)
0
尝试搜索在CodePlex上一口流利的路径...它给快捷方式使用lambda表达式目录内检索算法对文件/ LINQ
0
看一看的DirectoryInfo类。
你可能会需要一点递归的事情
1
你需要做的是在递归。请按照以下链接获取代码示例。
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;
}
相关问题
- 1. XML嵌套文件夹内容
- 2. 如何让MVC在嵌套文件夹中查找视图
- 3. 查找文件夹
- 4. 在嵌套文件结构中查找缺失的文件名
- 5. 查找嵌套主页内的控件
- 6. 批文件查找特定文件夹下的文件夹数
- 7. 使用嵌套文件夹遍历文件夹中的文件 - Cocoa
- 8. 嵌套文件夹中的web.config
- 9. 嵌套的文件夹结构
- 10. 查找文件夹中gzip文件内的字符串
- 11. 在文件夹和子文件夹中查找文件
- 12. 嵌套文件
- 13. Segfault用于嵌套文件夹调用
- 14. 从嵌套文件夹结构
- 15. URL路由和嵌套文件夹
- 16. PHP头重定向嵌套文件夹
- 17. angularjs嵌套ng-repeat文件夹结构
- 18. 将嵌套文件夹表示为DisplayObjects
- 19. PHP参数/嵌套文件夹/ CMS
- 20. 查找x(86))文件夹
- 21. 与生成文件嵌套的输出文件夹
- 22. 更改嵌套文件夹中文件的名称
- 23. 查找压缩文件夹内特定文件的文件路径
- 24. 计算文件夹内嵌套的级别
- 25. 计算文件夹内的文件夹
- 26. 查找父文件夹中的子文件夹
- 27. 查找tarred文件夹和普通文件夹的区别
- 28. Perl的重命名文件夹和文件文件::查找
- 29. Python的 - 文件内容嵌套列表
- 30. 创建嵌套文件夹到内部存储