遇到如图这样的问题:

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由子叶对象继承    我理解为“文件”可以指定该标志

相关文章:

  • 2022-01-04
  • 2021-10-27
  • 2022-01-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-27
猜你喜欢
  • 2021-09-21
  • 2022-12-23
  • 2022-12-23
  • 2021-12-20
  • 2022-02-01
  • 2021-09-29
  • 2022-12-23
相关资源
相似解决方案