对于那些不知道:一个结点的行为类似于一个符号链接在Linux上的文件夹.设置递归文件夹结构时会发生提到的陷阱,如下所示:
given folder /a/b
let /a/b/c point to /a
then
/a/b/c/b/c/b becomes valid folder locations.
Run Code Online (Sandbox Code Playgroud)
我建议像这样的策略.在Windows上,您被限制为路径字符串上的最大长度,因此递归解决方案可能不会破坏堆栈.
private void FindFilesRec(
string newRootFolder,
Predicate<FileInfo> fileMustBeProcessedP,
Action<FileInfo> processFile)
{
var rootDir = new DirectoryInfo(newRootFolder);
foreach (var file in from f in rootDir.GetFiles()
where fileMustBeProcessedP(f)
select f)
{
processFile(file);
}
foreach (var dir in from d in rootDir.GetDirectories()
where (d.Attributes & FileAttributes.ReparsePoint) != FileAttributes.ReparsePoint
select d)
{
FindFilesRec(
dir.FullName,
fileMustBeProcessedP,
processFile);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2128 次 |
| 最近记录: |