Set permissions for directory and child items

爷,独闯天下 提交于 2019-12-23 03:35:09

问题


My program to copy some directory, with subdirectories and files from server to local computer. I need, each local user can to modify it (edit/delete/remove/rename). But now it can do owner only. How can I set necessary permissions for copied directory, and its child items? I try such code:

String account = Path.Combine(Environment.MachineName, "Users");
FileSystemRights rights = FileSystemRights.FullControl;
AccessControlType controlType = AccessControlType.Allow;
DirectorySecurity security = local_commonDir.GetAccessControl(AccessControlSections.Access);
FileSystemAccessRule rule = new FileSystemAccessRule(account, rights, controlType);
security.AddAccessRule(rule);
local_commonDir.SetAccessControl(security);

But I got Exception:

Some or links to properties can't be transformed.

If I add absent Access Controls, then errors aren't present. I think that receiving an error because "Users" already exist. How to me to change the existing rights?


回答1:


I found solution:

WindowsIdentity id = WindowsIdentity.GetCurrent();
var sid = new SecurityIdentifier(WellKnownSidType.AccountDomainUsersSid, id.User.AccountDomainSid);
var security = dir.GetAccessControl();
var rule = new FileSystemAccessRule(sid,
    FileSystemRights.FullControl,
    InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
    PropagationFlags.None,
    AccessControlType.Allow);
security.AddAccessRule(rule);
dir.SetAccessControl(security);


来源:https://stackoverflow.com/questions/13591185/set-permissions-for-directory-and-child-items

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