添加“所有人”使用 C#.NET 的文件夹权限
我已使用下面的代码来允许“每个人”访问文件夹:
System.Security.AccessControl.DirectorySecurity sec =
System.IO.Directory.GetAccessControl(directory, AccessControlSections.All);
FileSystemAccessRule accRule = new FileSystemAccessRule("Everyone",
FileSystemRights.Modify,
AccessControlType.Allow);
sec.AddAccessRule(accRule); // setACL
sec.ResetAccessRule(accRule);
现在,“每个人”用户已添加到该文件夹,但未分配任何权限。所有读、写、执行等复选框均未选中。
I have used the code below to allow Everyone access to a folder:
System.Security.AccessControl.DirectorySecurity sec =
System.IO.Directory.GetAccessControl(directory, AccessControlSections.All);
FileSystemAccessRule accRule = new FileSystemAccessRule("Everyone",
FileSystemRights.Modify,
AccessControlType.Allow);
sec.AddAccessRule(accRule); // setACL
sec.ResetAccessRule(accRule);
Now, the Everyone user is added to the folder, but not with any rights assigned. All the read, write, execute etc. checkboxes are not checked.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想告诉你的第一件事是我是如何找到这个解决方案的。这可能比答案更重要,因为文件权限很难正确获得。
我做的第一件事是使用 Windows 对话框和复选框设置我想要的权限。我为“每个人”添加了一条规则,并勾选了除“完全控制”之外的所有框。
然后我编写了这段 C# 代码来准确地告诉我复制 Windows 设置需要哪些参数:
这给了我这行输出:
所以解决方案很简单(但如果您不知道要寻找什么,则很难找到正确的方法! ):
这将使 Windows 安全对话框上的复选框与您已为测试目录设置的复选框相匹配。
First thing I want to tell you is how I found this solution. This is probably more important than the answer because file permissions are hard to get correct.
First thing I did was set the permissions I wanted using the Windows dialogs and checkboxes. I added a rule for "Everyone" and ticked all boxes except "Full Control".
Then I wrote this C# code to tell me exactly what parameters I need to duplicate the Windows settings:
This gave me this line of output:
So the solution is simple (yet hard to get right if you don't know what to look for!):
This will make the checkboxes on the Windows security dialog match what you have already set for your test directory.
下面的代码检查文件夹是否存在,如果未创建,则创建一个。然后将该文件夹的每个用户权限设置为完全权限(读取和写入)。
The below code checks for the folder existence, if not created, creates one. And then sets every user- permission of that folder with full permission (read & write).
如果您想允许所有操作 (ACL),请使用
FileSystemRights.FullControl
而不是FileSystemRights.Modify
。use
FileSystemRights.FullControl
instead ofFileSystemRights.Modify
if you want to allow all actions (ACL).