在系统之后继续递归文件搜索. [英] Continue recursive file search after System."UnauthorizedAccessException"

查看:98
本文介绍了在系统之后继续递归文件搜索.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个您在其中指定根目录的程序,它将从该文件夹中递归搜索所有.mp4文件.

我的问题是,一旦找到它无权访问的文件夹,它就会停止搜索并且不会显示所有结果.

我的代码如下.

I''m trying to make a program where you specify a root directory and it will recursively search for all .mp4 files from that folder on.

My problem is that once it finds a folder it does not have access to, it just stops searching and doesn''t display all the results.

My code is as follows.

void DirSearch(string sDir)
{

    try
    {
        foreach (string d in Directory.GetDirectories(sDir))
        {

                     foreach (string f in Directory.GetFiles(d, "*.mp4"))
            {
                lstFilesFound.Items.Add(f);
            }
            DirSearch(d);
        }
    }
    catch (System.Exception excpt)
    {
        Console.WriteLine(excpt.Message);

    }
}



我需要把钓线放在哪里?

谢谢



Where do I need to put my catch line?

Thanks

推荐答案

我的问题是,一旦找到没有访问权限的文件夹,它就会停止搜索,并且不会显示所有结果.
所以?解决方案不在代码中.您需要确保文件夹具有访问权限集,以便您可以浏览文件夹.除非正确设置了安全特权,否则您将无法访问它们,并且会有未经授权的异常.
My problem is that once it finds a folder it does not have access to, it just stops searching and doesn''t display all the results.
So? The solution is not in the codes. You need to make sure that the folders have the access permission set such that you can browse through it. Until unless, security privileges are properly set, you cannot access them and will have unauthorized exception.


不幸的是,这是需要进行细粒度尝试的罕见情况之一, catch会阻止并阻止传播(如果发生异常)(但不会重新抛出).在这种方法中,您需要将try-catch放在包含每个文件系统元素的操作所在行的最小上下文中.这样,搜索将继续.我将解决为家庭练习编写代码的问题,这很简单.

但是,您也可以尝试递归GetFiles,区别之一是:
http://msdn.microsoft.com/en-us/library/ms143316.aspx [ ^ ];
使用System.SearchOption.AllDirectories:
http://msdn.microsoft.com/en-us/library/ms143448.aspx [ ^ ].



如果您确实要访问所有文件并使其中的大多数文件可访问,则还需要访问清单中的提升特权.用户将需要具有管理员访问权限并确认请求.

—SA
Unfortunately, this is one of the rare cases where you need to make a fine-grain try-catch block and block the propagation if exception (but not re-throwing). In this approach, you need to put your try-catch in the minimal context containing the line with the operation with each file system element. This way, the search will continue. I''ll lean the problem of writing code for your home exercise, this is very simple.

But also, you can try recursive GetFiles, the difference one:
http://msdn.microsoft.com/en-us/library/ms143316.aspx[^];
using System.SearchOption.AllDirectories:
http://msdn.microsoft.com/en-us/library/ms143448.aspx[^].



If you really want to access all files, and make most of them accessible, you would also need to access elevated privileges in the manifest. The user would need to have admin access and confirm the request.

—SA


这篇关于在系统之后继续递归文件搜索.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆