我的程序将一些目录、子目录和文件从服务器复制到本地计算机。我需要,每个本地用户都可以修改它(编辑/删除/删除/重命名)。但现在它只能做所有者。如何为复制的目录及其子项目设置必要的权限?
我尝试这样的代码:
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);
但我得到了例外:
某些属性或链接无法转换。
如果我添加缺少的访问控制,则不会出现错误。我认为收到错误是因为“用户”已经存在。我该如何改变现有的权利?
我找到了解决方案:
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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)