c# - 使用 C# 错误 "No flags can be set. Parameter name: inheritanceFlags"设置/更新文件/文件夹的继承标志权限

标签 c# inheritance permissions flags

我正在创建一个程序,它接受一个包含权限的文本文件并将这些权限应用于文件/文件夹。
当我尝试将继承标志更改为 ContainerInherit 或 ObjectInherit 时,我不断收到错误消息“无法设置任何标志。参数名称:inheritanceFlags”。
如果我将继承标志设置为无,我不会收到此错误。这是我的代码:

if (flag.Equals("None"))
{
    inheritFlag = InheritanceFlags.None;
}
else if (flag.Equals("ContainerInherit"))
{
    inheritFlag = InheritanceFlags.ContainerInherit;
}
else if (flag.Equals("ObjectInherit"))
{
    inheritFlag = InheritanceFlags.ObjectInherit;
}                          

FileSystemAccessRule fsarule = new FileSystemAccessRule(user, fileSysRight, inheritFlag, propFlag, controlType);
fileSec.AddAccessRule(fsarule);
File.SetAccessControl(currentFilePath, fileSec);

如何在不出现此错误的情况下设置继承标志?
谢谢

最佳答案

使用 DirectorySecurity 类而不是 File 类,因为不存在对文件的继承。它只接受目录和子目录和文件。我这样编辑你的代码:

DirectoryInfo dirInfo = new DirectoryInfo(folderPath);
DirectorySecurity dirSecurity = dirInfo.GetAccessControl();
dirSecurity.AddAccessRule(new FileSystemAccessRule(user, fileSysRight, inheritFlag, propFlag, controlType);
dirSecurity.SetAccessControl(currentFilePath, fileSec);

关于c# - 使用 C# 错误 "No flags can be set. Parameter name: inheritanceFlags"设置/更新文件/文件夹的继承标志权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23538876/

相关文章:

php - Ubuntu - 使用 file_put_contents 的正确权限

c# - 具有许多可选属性的对象的工厂模式

c# - Selenium Webdriver C# - 未选择 Firefox 下拉菜单文本

java - 没有封闭类型的实例...在范围内

ios - 从父级到子级的 Swift Pass 事件

mysql - 使用通配符设置用户 mysql 的权限?

git - 复制文件时 Homebrew 安装失败

c# - Entity Framework 4 中的 Linq 查询。糟糕的性能

c# - 在 xml 文件中查找值的最有效方法?

c++ - 如何将项目放入具有模板化父类(super class)类型的 vector 中