Cドライブでユーザーがテキストボックスに入力したフォルダーを検索するC#Windowsアプリプログラムを作成しました。ユーザーがボタンを押すと検索が開始されます。問題は、次のような例外が発生することです。
Access To The Path 'C:\Documents and Settings' is Denied
アクセス権のない C ドライブ (またはその他のドライブ) 内のフォルダー/ファイルを見つけて、C# プログラムを介してアクセス権を変更し、許可されたものなどにアクセスして、検索を続行できるようにするにはどうすればよいですか?
Linuxにはchmodがありますが、Windowsについてはわかりません..助けてください:)
検索コード:
string operationSucceeded = "Operation Completed Successfully";
Exception NoFilesFound = new Exception("No File(s) Found In Specified Directory");
List<string> foundFolders = new List<string>();
private void button5_Click(object sender, EventArgs e)
{
try
{
Search_In_For("C:\\", WantedFile_Folder_TextBox.Text);
if (foundFolders == null) throw NoFilesFound;
for (int i = 0; i < foundFolders.Count; i++)
SearchResultsTextBox.Text += (foundFolders[i] + "\n");
MessageBox.Show(operationSucceeded);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
delegate void SearchDelegate(string searchDir, string wanted);
private void Search_In_For(string searchDir, string wanted)
{
string[] foldersInThisDir = Directory.GetDirectories(searchDir);
if (foldersInThisDir.Length == 0) return;
for (int i = 0; i < foldersInThisDir.Length; i++)
if (foldersInThisDir[i].Contains(wanted))
foundFolders.Add(foldersInThisDir[i]);
SearchDelegate sd = new SearchDelegate(Search_In_For);
for (int i = 0; i < foldersInThisDir.Length; i++)
sd(foldersInThisDir[i] , wanted);
}