我正在创建一个程序,它接受一个包含权限的文本文件并将这些权限应用于文件/文件夹。
当我尝试将继承标志更改为 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/