c# - C#中的目录遍历

标签 c# ntfs

如何使用 C# 遍历文件夹结构而不落入 junction points 的陷阱? ?

最佳答案

对于那些不知道的人:连接点的行为类似于 Linux 上文件夹的符号链接(symbolic link)。当您设置递归文件夹结构时,就会出现上述陷阱,如下所示:

given folder /a/b
let /a/b/c point to /a
then
/a/b/c/b/c/b becomes valid folder locations.

我建议采用这样的策略。在 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);
    }
}

关于c# - C#中的目录遍历,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/301392/

上一篇:c# - 3路计算

下一篇:c# - 系统托盘访问

相关文章:

c# - 需要关闭网络请求流吗?

c# - 在 Mono 中加载 X509Certificate2 会引发 CryptographicException "Input data cannot be coded as a valid certificate"

windows - 如何在 NTFS 上创建稀疏文件?

java - NTFS 目录有 100K 个条目。如果分布在 100 个子目录中,性能会提升多少?

path - NTFS中的文件路径是否有长度限制?

c++ - 如何使用 C++ 读取 ntfs 主文件表

c# - 在 C# 中,如何从子类访问基类变量值

C# private void 到 public static void

c# - 无限循环消耗 100% CPU

windows - 这个字符串的含义\\.\c :