我制作了一个C#窗口应用程序,该程序将在C驱动器中搜索文件夹如果用户在文本框中输入,则搜索将在用户按下按钮后开始。问题是,我收到了一个异常,告诉我:
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);
}
尝试从Windows资源管理器运行程序"作为管理员"。