[英]Can't enable/disable ACE inheritance with SetAccessRuleProtection()
我正在使用以下代码启用/禁用 inheritance
public static void test(string filename, bool enabled)
{
FileSecurity security = File.GetAccessControl(filename);
security.SetAccessRuleProtection(enabled, enabled);
File.SetAccessControl(filename, security);
}
// disable inheritance and copy existing ACEs
test(true, true);
// enable inheritance
test(false, false);
但它不起作用,ACE 在资源管理器中保持不变。
什么问题?
你的代码在 Windows 10 上对我有用,虽然你肯定是指test("filename.txt", true)
而不是test(true, true)
不是吗? 我想知道您是否正在寻找 inheritance 的正确位置?
底部的代码在项目的 bin/debug 文件夹中创建一个 test.txt 文件,并在其上调用您的方法。 它表明 AreAccessRulesProtected 标志在调用之前从 False 变为 True,这意味着 inheritance 已被删除。
如果我在调用测试的行上中断,也就是说,在它被调用之前,并查看 Windows Explorer 中的文件属性,Security/Users/Advanced,然后我得到如下屏幕,显示权限是继承的:
如果我在通话后做同样的事情,我会看到下面的屏幕,其中显示 inheritance 已被删除:
代码:
static void Main(string[] args)
{
string fileName = "test.txt";
File.Delete(fileName);
File.WriteAllText(fileName, "Hello World");
bool rulesProtected = File.GetAccessControl(fileName).AreAccessRulesProtected;
Console.WriteLine($"AccessRulesProtected before setting protection: {rulesProtected}");
test(fileName, true);
rulesProtected = File.GetAccessControl(fileName).AreAccessRulesProtected;
Console.WriteLine($"AccessRulesProtected after setting protection: {rulesProtected}");
Console.ReadLine();
}
public static void test(string filename, bool enabled)
{
FileSecurity security = File.GetAccessControl(filename);
security.SetAccessRuleProtection(enabled, enabled);
File.SetAccessControl(filename, security);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.