简体   繁体   English

如何通过AC#程序在Windows中更改文件权限?

[英]How To Change File Permissions In Windows From A C# Program?

I made a C# windows App program that will search In the C drive for the folder that the user enters in a text box, the search will begin after the user presses a button. 我制作了一个C#Windows App程序,该程序将在C驱动器中搜索用户在文本框中输入的文件夹,然后在用户按下按钮后开始搜索。 The problem is that I'm getting an exception telling me: 问题是我收到一个异常消息:

Access To The Path 'C:\Documents and Settings' is Denied

How can I spot any folder/file in the C drive (or any other drive)that I don't have Access to, and change its permission via my C# program to access granted or something, so that I could continue my search ? 如何在C驱动器(或其他任何驱动器)中发现我没有访问权限的任何文件夹/文件,并通过C#程序更改其访问权限以授予权限或其他权限,以便我可以继续搜索?

In Linux there is chmod, but I don't know about windows .. Please Help :) 在Linux中有chmod,但我不知道Windows ..请帮助:)

The Search Code: 搜索代码:

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资源管理器“以管理员身份”运行程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过C#在Windows文件系统中的文件上的权限进行迭代 - Iterate through permissions on a file in the windows file system in C# 如何从 Windows 7 上的 C# 程序读取 Android 手机上的文件? - How to read files on Android phone from C# program on Windows 7? 如何使用C#从Windows服务运行EXE程序 - How to run an EXE program from a Windows Service using C# C#文件权限 - C# File Permissions 如何在C#中从Windows运行的程序(在Windows启动时运行)诊断用户运行的程序? - How to diagnose a program run by the user from a program run by Windows(run on windows startup)in C#? 如何通过C#应用程序在文件级别授予Windows权限? - How can I grant windows permissions at the file level via C# app? 如何从C#读取C程序生成的数据文件? - How to read a data file generated by a C program from C#? 更改文件权限-Windows Installer - Change file permissions - Windows Installer 如何授予SQL Server 2008数据库从C#Windows应用程序中的客户端系统插入数据的权限? - How to give the permissions to sql server 2008 database to insert the data from client system in c# windows application? 从C#控制台应用程序终止Windows进程:如何设置权限? - Killing a Windows process from a C# console application: how do I set permissions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM