SearchOption.AllDirectories 并忽略访问错误?

发布于 2024-09-30 20:28:30 字数 270 浏览 1 评论 0原文

        string[] files = Directory.GetFiles(tb_dir.Text, tb_filter.Text, SearchOption.AllDirectories);

我正在尝试搜索目录和所有子目录以查找某些文件。我不断遇到当前代码的错误,第二个它看到无法进入的内容就中断了,

在这个应用程序中,这并不重要,我宁愿它继续前进。有没有办法绕过这个代码,避免每次都转储出来?

谢谢

崩溃893

        string[] files = Directory.GetFiles(tb_dir.Text, tb_filter.Text, SearchOption.AllDirectories);

I'm trying to search through a directory and all sub directory to find some file. I keep running into an error with the current code that the second it sees something it can't get into it breaks

In this application that doesn't matter i would rather it just move on. Is there anyway to bypass this code from dumping out everytime?

Thanks

Crash893

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

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

发布评论

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

评论(1

手长情犹 2024-10-07 20:28:30

你可以这样做:

List<string> GetFiles(string topDirectory, string filter)
{
    List<string> list = new List<string>();
    list.AddRange(Directory.GetFiles(topDirectory, filter));
    foreach (string directory in Directory.GetDirectories(topDirectory))
    {
        list.AddRange(GetFiles(directory));
    }
    return list;
}

并用以下方式调用它:

List<string> files = GetFiles(tb_dir.Text, tb_filter.Text);

当然,你可以将文件列表转换为数组。

您必须添加 try catch 块来处理 UnauthorizedAccessException。

You could do something like this:

List<string> GetFiles(string topDirectory, string filter)
{
    List<string> list = new List<string>();
    list.AddRange(Directory.GetFiles(topDirectory, filter));
    foreach (string directory in Directory.GetDirectories(topDirectory))
    {
        list.AddRange(GetFiles(directory));
    }
    return list;
}

and call it with:

List<string> files = GetFiles(tb_dir.Text, tb_filter.Text);

You could convert the list of files into an array, of course.

You would have to add try catch blocks to handle the UnauthorizedAccessException.

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