我正在尝试读取与当前文件权限无关的完整目录。但即使我拥有“SeBackupPrivilege”,以下代码也会导致“UnauthorizedAccessException”。这是怎么回事?
//Create the test-directory.
string testPath = Path.Combine(Environment.CurrentDirectory, "TestDenied");
string filePath = Path.Combine(testPath, "Foo.txt");
Directory.CreateDirectory(testPath);
using (var fs = File.CreateText(filePath)) {
fs.WriteLine("Foo");
}
var ds = Directory.GetAccessControl(testPath, Utils.AccessControlSectionsToRead);
ds.SetOwner(WindowsIdentity.GetCurrent().User);
ds.AddAccessRule((FileSystemAccessRule)ds.AccessRuleFactory(
WindowsIdentity.GetCurrent().User,
(int)FileSystemRights.FullControl,
false,
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
PropagationFlags.None,
AccessControlType.Deny)
);
Directory.SetAccessControl(testPath, ds);
//Get the backup-privilege
WinAPI.ModifyPrivilege(PrivilegeName.SeBackupPrivilege, true);
//Checked the privilege on the command line here: The process has it.
//Try to access the forbidden file.
using (var fs = File.OpenRead(filePath)) {
//UnauthorizedAccessException from the line above.
}
怎么会这样?我以为 SeBackupPrivilege 可以让我访问所有文件?