C# 在嵌套文件夹中查找文件
如何搜索嵌套文件夹中的文件?尝试在codeplex中搜索流畅路径…它为使用lambdas/linq的目录中的文件提供serach的快捷方式尝试在codeplex中搜索流畅路径…它为使用lambdas/linq的目录中的文件提供serach的快捷方式查看该类 您可能需要进行一点递归,看看这个类C# 在嵌套文件夹中查找文件,c#,asp.net,C#,Asp.net,如何搜索嵌套文件夹中的文件?尝试在codeplex中搜索流畅路径…它为使用lambdas/linq的目录中的文件提供serach的快捷方式尝试在codeplex中搜索流畅路径…它为使用lambdas/linq的目录中的文件提供serach的快捷方式查看该类 您可能需要进行一点递归,看看这个类 您可能需要进行一些递归操作您需要以递归方式进行操作。按照下面的链接获取代码示例 您需要以递归的方式进行操作。按照下面的链接获取代码示例 带有Linq和目录。枚举文件 var files = from
您可能需要进行一些递归操作您需要以递归方式进行操作。按照下面的链接获取代码示例
您需要以递归的方式进行操作。按照下面的链接获取代码示例
带有Linq和目录。枚举文件
var files =
from file in Directory.EnumerateFiles(rootFolder,searchFor,SearchOption.AllDirectories)
select file;
使用Linq和目录。枚举文件
var files =
from file in Directory.EnumerateFiles(rootFolder,searchFor,SearchOption.AllDirectories)
select file;
看看这个函数
System.IO.DirectoryInfo.GetFiles("SearchPattern",SearchOption.AllDirectories)
看看这个函数
System.IO.DirectoryInfo.GetFiles("SearchPattern",SearchOption.AllDirectories)
使用递归
编写在特定文件夹中搜索文件的方法。
从每个子目录的内部调用该方法,如果找到该文件,则让它返回路径
伪C代码(仅用于获取想法):
这将遍历所有子目录,并返回搜索文件的路径(如果在其中),否则返回空字符串。使用递归
编写在特定文件夹中搜索文件的方法。
从每个子目录的内部调用该方法,如果找到该文件,则让它返回路径
伪C代码(仅用于获取想法):
这将在所有子目录中运行,并返回搜索文件的路径(如果在其中),否则返回空字符串。尝试以下操作:
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;
}
试试这个:
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;
}