问题
- UnAuthorizedAccessException:递归搜索目录时,例如 C:\
A “访问路径 '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());
}
}
}
}