C# 中的目录遍历

发布于 2024-07-09 06:41:01 字数 122 浏览 9 评论 0原文

如何使用 C# 遍历文件夹结构而不陷入连接点的陷阱?

How do you traverse a folder structure using C# without falling into the trap of junction points?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄与心 2024-07-16 06:41:01

对于那些不知道的人:连接点的行为类似于 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);
    }
}

For those that don't know: A junction point behaves similarly to a symbolic link for a folder on linux. The trap that is mentioned happens when you set up a recursive folder structure, like this:

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

I suggest a strategy like this one. On windows you are limited to a maximum length on the path string, so a recursive solution probably won't blow the stack.

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);
    }
}
仲春光 2024-07-16 06:41:01

您可以使用以下代码:

private void processing(string directory)
        {
            cmbFilesTypesSelectedIndex = cmbFilesTypes.SelectedIndex;
            CheckForProjectFile(directory);
            DirectoryInfo dInfo = new DirectoryInfo(directory);
            DirectoryInfo[] dirs = dInfo.GetDirectories() ;
            foreach (DirectoryInfo subDir in dirs)
            {
                CheckForProjectFile(subDir.FullName);
                processing(subDir.FullName);
            }
        }

        private void CheckForProjectFile(string directory)
        {
            Boolean flag = false; 
            DirectoryInfo dirInfo = new DirectoryInfo(directory);
            FileInfo[] files = dirInfo.GetFiles();
            //You can also traverse in files also
            foreach (FileInfo subfile in files)
            {
                //Do you want

            }
        }

you can use following code:

private void processing(string directory)
        {
            cmbFilesTypesSelectedIndex = cmbFilesTypes.SelectedIndex;
            CheckForProjectFile(directory);
            DirectoryInfo dInfo = new DirectoryInfo(directory);
            DirectoryInfo[] dirs = dInfo.GetDirectories() ;
            foreach (DirectoryInfo subDir in dirs)
            {
                CheckForProjectFile(subDir.FullName);
                processing(subDir.FullName);
            }
        }

        private void CheckForProjectFile(string directory)
        {
            Boolean flag = false; 
            DirectoryInfo dirInfo = new DirectoryInfo(directory);
            FileInfo[] files = dirInfo.GetFiles();
            //You can also traverse in files also
            foreach (FileInfo subfile in files)
            {
                //Do you want

            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文