简体   繁体   中英

Set permissions for directory and child items

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?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM