相关疑难解决方法(0)

为什么我不能在C#中设置此ACL规则?

在Vista SP1上作为提升管理员运行,我的C#应用​​程序尝试使用以下代码设置以下规则.不会产生错误,但目录的ACL上也没有任何更改.我错过了什么?

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);
}
Run Code Online (Sandbox Code Playgroud)

更新:只需添加以下代码作为_SetAcl方法的最后一行,我的代码就可以了.

info.SetAccessControl(acl);
Run Code Online (Sandbox Code Playgroud)

.net c# security rights acl

2
推荐指数
1
解决办法
2439
查看次数

标签 统计

.net ×1

acl ×1

c# ×1

rights ×1

security ×1