加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C#中的目录遍历

发布时间:2020-12-15 07:44:07 所属栏目:百科 来源:网络整理
导读:如何使用C#遍历文件夹结构而不会陷入 junction points的陷阱? 解决方法 对于那些不知道的人:连接点的行为类似于linux上文件夹的符号链接.设置递归文件夹结构时会发生提到的陷阱,如下所示: given folder /a/blet /a/b/c point to /athen/a/b/c/b/c/b becom
如何使用C#遍历文件夹结构而不会陷入 junction points的陷阱?

解决方法

对于那些不知道的人:连接点的行为类似于linux上文件夹的符号链接.设置递归文件夹结构时会发生提到的陷阱,如下所示:
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);
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读