【发布时间】:2011-06-16 09:26:19
【问题描述】:
问题
- UnAuthorizedAccessException:递归搜索目录时,例如 C:\
“访问路径 'c:\Documents and Settings\' 被拒绝。”即使在 UAC 特权升级和管理员组访问权限的情况下也会发生。
尝试的方法
- Try & Catch:使用其中一种方法(异常、UnAuthorizedAccessException、Blank Catch、继续)
问题
- 如何处理这种异常并继续正常运行程序?这需要对非管理员和管理员帐户都有效。
示例代码
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());
}
}
}
}
【问题讨论】:
-
据我所知,不允许以编程方式访问文档和设置。甚至不是超级管理员
-
@Theun:什么是“超级管理员”?
-
@Theun:我假设您在谈论隐藏管理员(内置)帐户。我没有尝试过,但这应该可以让您不受限制地访问系统。我不需要那个。只是在寻找一种方法来绕过我的搜索被这种烦人的未经授权的访问废话所阻止。
标签: c# exception windows-7 unauthorized