如何在 Windows 7/Server 2008 中以编程方式编辑主机文件?
我正在编写一个小 WPF 实用程序来管理主机文件中的条目以用于开发目的。您可能知道主机文件受较新操作系统(Win 7/2008/Vista)的保护。
我已在我的应用程序中添加了一个清单,以将 requestsExecutionLevel 设置为“requireAdministrator”,详细信息 此处(使用“简单方法”)和相关问题此处。
不幸的是这对我不起作用。启动应用程序时没有提升提示,并且为hosts文件调用File.AppendText仍然会导致抛出System.UnauthorizedAccessException:“访问路径'C:\Windows\System32\drivers\etc\hosts'是被拒绝了。”
HostsChanger.exe.manifest:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity version="1.0.0.0" processorArchitecture="X86" name="HostsChanger" type="win32"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="requireAdministrator"/>
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
有什么想法吗?
I'm writing a little WPF utility to manage entries in the hosts file for dev purposes. As you might know the hosts file is protected by the newer OSs (Win 7/2008/Vista).
I've added a manifest to my application to set the requestedExecutionLevel to "requireAdministrator", as detailed here (using "the easy way") and in the related question here.
Unfortunately this has not worked for me. There is no elevation prompt when I start the app, and calling File.AppendText for the hosts file still causes a System.UnauthorizedAccessException to be thrown: "Access to the path 'C:\Windows\System32\drivers\etc\hosts' is denied."
HostsChanger.exe.manifest:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity version="1.0.0.0" processorArchitecture="X86" name="HostsChanger" type="win32"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="requireAdministrator"/>
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
转述我之前的评论,变成了一个答案:
ho1给出的答案包含一个app.manifest,它与我正在工作的应用程序完全相同,并且elevation正在为它工作。此处的区别在于文件名是“app.manifest”,项目选项“Manifest”(在“应用程序”选项卡上)指向它。
Paraphrased from my earlier comment, turned into an answer:
The answer ho1 gave contains an app.manifest that is exactly the same as the app I'm working on at work, and elevation is working for it. The difference here is that the filename is "app.manifest", and the project option "Manifest" (on the Application tab) is pointing to it.
我不确定它是否会产生任何影响,但您的清单片段与我对它应该如何的理解略有不同(尽管可能是不同的版本):
否则,解决方法可能是有一个单独的“加载器”用户启动的应用程序,仅使用
Verb
runas
启动真正的 WPF 工具,如 此 博客文章(因此Process.StartInfo.Verb = "runas";
) 。I'm not sure if it'll make any difference but your manifest snippet is slightly different from my understanding of how it should be (though that might be different versions):
Otherwise, a work around might be to have a separate "Loader" app that the user starts with and that only starts your real WPF tool using the
Verb
runas
as detailed in this blog post (soProcess.StartInfo.Verb = "runas";
).我要在这里摸索一下,说这是一个authenticode签名问题。我没有听到您提到任何有关签署申请的事情。据我了解,与 Vista 不同,在 Windows 2008/7 中,运行提升的应用程序的唯一方法是拥有一个已签名的应用程序清单,用于标识应用程序所需的权限级别。如果您需要签名方面的帮助,请参阅以下有关如何对应用程序进行签名的文章:http: //msdn.microsoft.com/en-us/library/bb756995.aspx
I am going to take a stab in the dark here and say that it is an authenticode signature issue. I have not heard you mention anything about signing your application. As far as my understanding goes, unlike Vista, in Windows 2008/7 the only way to run an application elevated is to have a signed application manifest that identifies the privilege level that the application needs. If you need help signing, here's an article on how to sign your application: http://msdn.microsoft.com/en-us/library/bb756995.aspx