![](/img/trans.png)
[英]"Attempted to perform an unauthorized operation" when calling NamedPipeServerStream.SetAccessControl
[英]SetAccessControl “unauthorized operation” even when run as administrator
我试图获得所有权并更改C#中文件的ACL,但是即使以管理员身份,我也遇到了例外:
System.UnauthorizedAccessException: Attempted to perform an unauthorized operation.
运行该程序的用户可以通过Windows界面获得所有权和更改权限。
我的代码:
string fileName = @"C:\temp\mount\Windows\System32\Boot\en-US\winload.exe.mui";
FileSecurity fileSec = File.GetAccessControl(fileName);
fileSec.SetOwner(WindowsIdentity.GetCurrent().User);
File.SetAccessControl(fileName, fileSec); //exception thrown here
我什至添加了一项检查以确保当前用户是管理员组的成员:
WindowsIdentity wi = WindowsIdentity.GetCurrent();
WindowsPrincipal wp = new WindowsPrincipal(wi);
bool isAdmin = wp.IsInRole(WindowsBuiltInRole.Administrator); //returns true
背景信息:我正在创建WinPE映像,需要替换winload.exe.mui文件。
另外,对该文件的当前权限仅授予对“受信任的安装程序”的完全访问权限。
我在Windows 7上运行
我通过使用System.Diagnostics.Process
在命令外壳中运行takeown
解决了此问题。 然后,我可以无误地设置访问控制。
奇怪的是takeown
但是等效的.NET库却没有。
您仍然可以在新方法中使用File.SetAccessControl()
代替FileStream.SetAccessControl()
。 我愿意打赌它也会起作用。 MSDN实际上建议这种做法:
“虽然可以在现有文件上使用FileStream类和SetAccessControl,但请考虑使用File.SetAccessControl方法,因为它更易于使用。”
http://msdn.microsoft.com/zh-cn/library/system.io.filestream.setaccesscontrol.aspx[^]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.