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

荒凉一梦 提交于 2019-12-08 08:59:50

问题


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.


回答1:


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.




回答2:


You can use FileInfo.GetAccessControl and File.SetAccessControl methods

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



来源:https://stackoverflow.com/questions/17763570/change-a-files-acl-to-allow-full-access-for-everyone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!