Why can I not set this ACL rule in C#?

主宰稳场 提交于 2019-12-19 10:14:31

问题


Running as an elevated admin on Vista SP1, my C# app tries to set the following rule with the following code. No error is produced, but neither is any change on the directory's ACL. What am I missing?

public static void Main( string args[] )
{
    string dirPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Company"), "Product" );
    Directory.Create(dirPath);
    _SetAcl(dirPath, "Users", FileSystemRights.FullControl);
}

private static void _SetAcl(string path, string identity, FileSystemRights rights)
{
    var info = new DirectoryInfo(path);
    var acl = info.GetAccessControl();

    var rule1 = new FileSystemAccessRule(identity, rights, AccessControlType.Allow);
    bool modified;
    acl.ModifyAccessRule(AccessControlModification.Reset, rule1, out modified);

    var inheritanceFlags = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;
    var rule2 = new FileSystemAccessRule(identity, rights, inheritanceFlags,
                                        PropagationFlags.InheritOnly, AccessControlType.Allow);
    acl.ModifyAccessRule(AccessControlModification.Add, rule2, out modified);
}

Update: Just add the following code as the last line of the _SetAcl method, and my code is good to go.

info.SetAccessControl(acl);

回答1:


To finish the process you must call DirectoryInfo.SetAccessControl() with the modified ACL.

GetAccessControl() really returns a copy of the ACL. You're free to modify it but it won't take effect until you call SetAccessControl()



来源:https://stackoverflow.com/questions/507124/why-can-i-not-set-this-acl-rule-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!