使用此代码,我可以从FOLDER读取文件,但我想读取放在子文件夹中的txt文件
前:
D:\Folder\NewFolder\Txt
D:\Folder\NewFolder1\Txt
D:\Folder\NewFolder2\Txt
我想阅读txt文件" Txt"文件夹 " TXT"文件夹在所有子文件夹中都很常见
Dictionary<string, string> found = new Dictionary<string, string>();
string line;
foreach (string filename in Directory.GetFiles(path.text))
{
using (StreamReader file = new StreamReader(filename))
{
while ((line = file.ReadLine()) != null)
{
if (line.Contains("Errors: 1"))
{
found.Add(line, filename);
label1.Text = "Failed \n" + filename;
label1.ForeColor = System.Drawing.Color.Red;
break;
}
}
}
}
答案 0 :(得分:0)
创建所有文件夹的列表并迭代:
public List<String> files = new List<String>();
private void DirSearch(string sDir)
{
foreach (string f in Directory.GetFiles(sDir))
files.Add(f);
foreach (string d in Directory.GetDirectories(sDir))
DirSearch(d);
}