目录:在C#中完全控制

时间:2012-05-21 08:15:09

标签: c# winforms

我在C#中使用此代码来完全控制任何文件,它运行良好。

但是如何完全控制任何目录呢?

允许完全访问文件的代码:

string fileName = @"d:\MyFile.mdb";
FileSecurity fSecurity = File.GetAccessControl(fileName);
fSecurity.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, AccessControlType.Allow));
File.SetAccessControl(fileName, fSecurity);

2 个答案:

答案 0 :(得分:3)

以完全相同的方式,但使用Directory.GetAccessControl代替File.GetAccessControl

DirectorySecurity dirSec = Directory.GetAccessControl("C:\\temp");
dirSec.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, AccessControlType.Allow));
Directory.SetAccessControl("c:\\temp", dirSec);

答案 1 :(得分:0)

以下是一个例子:

        DirectoryInfo myDirectoryInfo = new DirectoryInfo(@"C:\Dir1\");

        DirectorySecurity myDirectorySecurity = myDirectoryInfo.GetAccessControl();

        string User = System.Environment.UserDomainName + "\\" + "Everyone"; 

        myDirectorySecurity.AddAccessRule(new FileSystemAccessRule(User, 
                                          FileSystemRights.FullControl, 
                                          AccessControlType.Deny)); 

        myDirectoryInfo.SetAccessControl(myDirectorySecurity);