【问题标题】:How To Change File Permissions In Windows From A C# Program?如何从 C# 程序更改 Windows 中的文件权限?
【发布时间】:2012-07-14 02:52:48
【问题描述】:

我制作了一个 C# windows App 程序,它将在 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);
}

【问题讨论】:

标签: c# file-permissions directory-permissions


【解决方案1】:

尝试从 Windows 资源管理器“以管理员身份”运行您的程序。

【讨论】:

  • 没用,这实际上与我正在尝试做的事情无关
  • 好的,这个链接可能会有所帮助:en.wikipedia.org/wiki/…
猜你喜欢
  • 1970-01-01
  • 2013-12-30
  • 2017-10-12
  • 2014-08-28
  • 2013-03-19
  • 1970-01-01
  • 2010-10-02
  • 2019-09-26
  • 2020-06-10
相关资源
最近更新 更多