我的程序用于复制某些目录,子目录和文件从服务器到本地计算机。我需要,每个本地用户都可以修改它(编辑/删除/删除/重命名)。但现在它只能做主人。如何为复制的目录及其子项设置必要的权限? 我尝试这样的代码:
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);
但我得到了例外:
无法转换部分或属性链接。
如果我添加缺少访问控制,则不存在错误。我认为因为“用户”已经存在而收到错误。如何更改现有权利?
答案 0 :(得分:3)
我找到了解决方案:
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);