問題
- UnAuthorizedAccessException: C:\ などのディレクトリを再帰的に検索する
と、「パス 'c:\Documents and Settings\' へのアクセスが拒否されました。」UAC Priveledges がアップグレードされ、管理者グループ アクセスがある場合でも発生します。
試みられた方法
- Try & Catch: これらのメソッドのいずれかを使用 (Exception、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());
}
}
}
}