我设法从“root”文件夹子目录中获取文件,但我也从这些子目录directory2获取文件,我不想这样做。
示例:RootDirectory>Subdirectories (wanted files)>directories2 (unwanted files)
我用过这段代码:
public void ReadDirectoryContent()
{
var s1 = Directory.GetFiles(RootDirectory, "*", SearchOption.AllDirectories);
{
for (int i = 0; i <= s1.Length - 1; i++)
FileInfo f = new FileInfo(s1[i]);
. . . etc
}
}
答案 0 :(得分:6)
试试这个:
var filesInDirectSubDirs = Directory.GetDirectories(RootDirectory)
.SelectMany(d=>Directory.GetFiles(d));
foreach(var file in filesInDirectSubDirs)
{
// Do something with the file
var fi = new FileInfo(file);
ProcessFile(fi);
}
我们的想法是首先选择第一级子目录,然后使用Enumerable.SelectMany方法“汇总”所有文件