C#修改文件的安全属性时报“没有可以设置的标志”

遇到如图这样的问题:

造成这样的错误原因是:

原来的代码:

    void ModifyFileSecurityInfo(string filename, string username)
        {

            System.IO.FileInfo fileInfo = new FileInfo(filename);
            FileSecurity fs = fileInfo.GetAccessControl();
            fs.AddAccessRule(new FileSystemAccessRule(username, FileSystemRights.Modify, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
            fileInfo.SetAccessControl(fs);
        }

改后的代码:

    void ModifyFileSecurityInfo(string filename, string username)
        {

            System.IO.FileInfo fileInfo = new FileInfo(filename);
            FileSecurity fs = fileInfo.GetAccessControl();
            fs.AddAccessRule(new FileSystemAccessRule(username, FileSystemRights.Modify, InheritanceFlags.None, PropagationFlags.None, AccessControlType.Allow));
            fileInfo.SetAccessControl(fs);
        }

InheritanceFlags的解释是:指定访问控制项(ACE)的继承语义。

有三个枚举:

None 子对象未继承ACE

ContainerInherit 由容器子对象继承   我理解为“文件夹”可以指定该标志

ObjectInherit由子叶对象继承    我理解为“文件”可以指定该标志

原文地址:https://www.cnblogs.com/langu/p/2827424.html