c#中如何查找一个目录下(包含子目录)所有文件中包含某一字符串的所有文件

2024-11-02 19:32:21
推荐回答(1个)
回答1:

用递归

private void getDirectories(string path)
{
string[] fileNames = Directory.GetFiles(path);
string[] directories = Directory.GetDirectories(path);
foreach (string file in fileNames)
{
Console.WriteLine("Filename:{0}",file);
}
foreach (string dir in directories)
{
Console.WriteLine("Directoriesname:{0}",dir);
getDirectories(dir);
}
}

Console.WriteLine这个地方你可以换成arraylist