本文介绍了如何从确切的子目录获取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我设法从根"文件夹子目录中获取文件,但是我也从这些子目录directory2中获取文件,我不想这样做.
I've managed to get files out of "root" folder subdirectories, but I also get files from these subdirectories directories2, which I don't want to.
示例: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
}
}
推荐答案
尝试一下:
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 方法
The idea is to first select 1st level of subdirectories, then "aggregate" all files using Enumerable.SelectMany method
这篇关于如何从确切的子目录获取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!