.net 中的递归文件搜索

发布于 2024-07-10 21:46:56 字数 244 浏览 7 评论 0原文

我需要在驱动器(C:、D: 等)中搜索特定文件类型(扩展名如 .xml、.csv、.xls)。 如何执行递归搜索以循环所有目录和内部目录并返回文件所在位置的完整路径? 或者我可以从哪里获得这方面的信息?

VB.NET 或 C#

谢谢

编辑〜我遇到了一些错误,例如无法访问系统卷访问被拒绝等。有谁知道我在哪里可以看到一些实现文件搜索的示例代码? 我只需要搜索选定的驱动器并返回找到的所有文件的文件类型的完整路径。

I need to search a drive (C:, D: etc) for a partuicular file type (extension like .xml, .csv, .xls). How do I preform a recursive search to loop all directories and inner directories and return the full path of where the file(s) are? or where can I get information on this?

VB.NET or C#

Thanks

Edit ~ I am running into some errors like unable to access system volume access denied etc. Does anyone know where I can see some smaple code on implementing a file search? I just need to search a selected drive and return the full path of the file type for all the files found.

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

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

发布评论

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

评论(3

假扮的天使 2024-07-17 21:46:57
System.IO.Directory.GetFiles(@"c:\", "*.xml", SearchOption.AllDirectories);
System.IO.Directory.GetFiles(@"c:\", "*.xml", SearchOption.AllDirectories);
紅太極 2024-07-17 21:46:57

这个怎么样? 它避免了内置递归搜索经常引发的异常(即,您对单个文件夹的访问被拒绝,并且整个搜索失败),并且是惰性评估的(即,它在找到结果后立即返回结果,而不是缓冲 2000 个结果)。 惰性行为使您可以构建响应式 UI 等,并且还可以与 LINQ 配合使用(尤其是 First()Take() 等)。

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
static class Program { // formatted for vertical space
    static void Main() {
        foreach (string match in Search("c:\\", "*.xml")) {
            Console.WriteLine(match);
        }
    }
    static IEnumerable<string> Search(string root, string searchPattern) {
        Queue<string> dirs = new Queue<string>();
        dirs.Enqueue(root);
        while (dirs.Count > 0) {
            string dir = dirs.Dequeue();

            // files
            string[] paths = null;
            try {
                paths = Directory.GetFiles(dir, searchPattern);
            } catch { } // swallow

            if (paths != null && paths.Length > 0) {
                foreach (string file in paths) {
                    yield return file;
                }
            }

            // sub-directories
            paths = null;
            try {
                paths = Directory.GetDirectories(dir);
            } catch { } // swallow

            if (paths != null && paths.Length > 0) {
                foreach (string subDir in paths) {
                    dirs.Enqueue(subDir);
                }
            }
        }
    }
}

How about this? It avoids the exception often thrown by the in-built recursive search (i.e. you get access-denied to a single folder, and your whole search dies), and is lazily evaluated (i.e. it returns results as soon as it finds them, rather than buffering 2000 results). The lazy behaviour lets you build responsive UIs etc, and also works well with LINQ (especially First(), Take(), etc).

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
static class Program { // formatted for vertical space
    static void Main() {
        foreach (string match in Search("c:\\", "*.xml")) {
            Console.WriteLine(match);
        }
    }
    static IEnumerable<string> Search(string root, string searchPattern) {
        Queue<string> dirs = new Queue<string>();
        dirs.Enqueue(root);
        while (dirs.Count > 0) {
            string dir = dirs.Dequeue();

            // files
            string[] paths = null;
            try {
                paths = Directory.GetFiles(dir, searchPattern);
            } catch { } // swallow

            if (paths != null && paths.Length > 0) {
                foreach (string file in paths) {
                    yield return file;
                }
            }

            // sub-directories
            paths = null;
            try {
                paths = Directory.GetDirectories(dir);
            } catch { } // swallow

            if (paths != null && paths.Length > 0) {
                foreach (string subDir in paths) {
                    dirs.Enqueue(subDir);
                }
            }
        }
    }
}
人间☆小暴躁 2024-07-17 21:46:57

它看起来像 recls 库 - 代表 recursive ls - 现在有一个纯 .NET 实现。 我刚刚在 Dobb 博士的文章中读到了相关内容

将用作:

using Recls;
using System;
static class Program { // formatted for vertical space
    static void Main() {
        foreach(IEntry e in FileSearcher.Search(@"c:\", "*.xml|*.csv|*.xls")) {
            Console.WriteLine(e.Path);
        }
    }

It looks like the recls library - stands for recursive ls - now has a pure .NET implementation. I just read about it in Dr Dobb's.

Would be used as:

using Recls;
using System;
static class Program { // formatted for vertical space
    static void Main() {
        foreach(IEntry e in FileSearcher.Search(@"c:\", "*.xml|*.csv|*.xls")) {
            Console.WriteLine(e.Path);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文