Set permissions for directory and child items

2019-06-07 02:32发布

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条回答
ら.Afraid
2楼-- · 2019-06-07 03:18

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);
查看更多
登录 后发表回答