2

我的程序将一些目录、子目录和文件从服务器复制到本地计算机。我需要,每个本地用户都可以修改它(编辑/删除/删除/重命名)。但现在它只能做所有者。如何为复制的目录及其子项设置必要的权限?我尝试这样的代码:

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

但我得到了例外:

某些属性或指向属性的链接无法转换。

如果我添加不存在的访问控制,则不存在错误。我认为收到错误是因为“用户”已经存在。我如何更改现有权利?

4

1 回答 1

4

我找到了解决方案:

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);
于 2012-12-06T10:22:54.967 回答