Getfiles()UnAuthorizedAccessAccept在WIN7中 [英] Getfiles() UnAuthorizedAccessAcception in WIN7

查看:167
本文介绍了Getfiles()UnAuthorizedAccessAccept在WIN7中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题


  1. UnAuthorizedAccessException:递归搜索目录,如C:\

    访问路径c:\Documents和Settings \被拒绝。即使与UAC Priveledges升级&管理员组访问。

尝试方法


  1. 尝试&捕获:使用这些方法之一(异常,UnAuthorizedAccessException,空白捕捉,继续)

问题 br>


  1. 如何处理这种异常并继续正常运行程序?这需要在非管理员和管理员帐户上工作。

示例代码

using System;
using System.IO;

namespace filecheck
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 0;
            int html = 0;
            try
            {
                string[] filePaths = Directory.GetFiles(@"c:\", "*.html", SearchOption.AllDirectories);

                foreach (string files in filePaths)
                {
                    if (Convert.ToBoolean(files.IndexOf("html")))
                    {
                        html++;
                    }
                    Console.WriteLine(files);
                    i++;

                }
                Console.Write("# Files found: {0} Html: {1)", i, html);
            }
            catch (UnauthorizedAccessException e)
            {
                Console.WriteLine("The process failed: {0}", e.ToString());
            }
            catch (Exception e)
            {
                Console.WriteLine("The process failed: {0}", e.ToString());

            }

        }
    }
}


推荐答案

不幸的是,手动执行递归的唯一方法就是处理这个问题。即使在微软自己的示例代码中,他们也是这样做的,只是为了避免整个搜索失败,因为一个或多个目录无法访问。

Unfortunately the only way to handle this is by doing the recursion manually. Even in Microsoft's own sample code they do it this way, just to avoid that the whole search fails because one or more directories can not be accessed.

换句话说,只有使用 SearchOption.AllDirectories 重新搜索您确定的目录的有限子集不会包含您无法访问的任何目录。

So in other words, only use SearchOption.AllDirectories when you're searching a limited subset of directories which you're certain won't contain any directories which you won't have access to.

这篇关于Getfiles()UnAuthorizedAccessAccept在WIN7中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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