3

我在 C# 中有这段代码,可以完全控制任何文件,效果很好。

但是我怎样才能完全控制任何目录呢?

提供对文件的完全访问权限的代码:

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

2 回答 2

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);
于 2012-05-21T08:18:38.867 回答
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);
于 2012-05-21T08:18:05.807 回答