Change a File's ACL to Allow Full Access for E

2019-06-08 21:44发布

How do I change a file's owner to Everyone and also allow the Everyone object Full Access?

Are there any APIs available for this? Do I have to use P/Invoke?

I searched everywhere but can't find anything to do this.

2条回答
forever°为你锁心
2楼-- · 2019-06-08 22:08

You can use FileInfo.GetAccessControl and File.SetAccessControl methods

check the sample code given in MSDN link for File.SetAccessControl

查看更多
虎瘦雄心在
3楼-- · 2019-06-08 22:11

Indeed, there are API's available for this. You may want to have a look at the File.SetAccessControl method in the System.IO namespace.

// Read the current ACL details for the file
var fileSecurity = File.GetAccessControl(fileName);

// Create a new rule set, based on "Everyone"
var fileAccessRule = new FileSystemAccessRule(new NTAccount("", "Everyone"),
      FileSystemRights.FullControl, 
      AccessControlType.Allow);

// Append the new rule set to the file
fileSecurity.AddAccessRule(fileAccessRule);

// And persist it to the filesystem
File.SetAccessControl(fileName, fileSecurity);

The forementioned article has tons of great things to play with, regarding the ACL.

查看更多
登录 后发表回答