如何在 Windows 7/Server 2008 中以编程方式编辑主机文件?

发布于 2024-09-08 19:15:30 字数 1158 浏览 8 评论 0原文

我正在编写一个小 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深海不蓝 2024-09-15 19:15:30

转述我之前的评论,变成了一个答案:

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.

ζ澈沫 2024-09-15 19:15:30

我不确定它是否会产生任何影响,但您的清单片段与我对它应该如何的理解略有不同(尽管可能是不同的版本):

<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <assemblyIdentity version="1.0.0.0" name="HostsChanger" />
   <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
      <security>
         <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
            <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
         </requestedPrivileges>
      </security>
   </trustInfo>
</asmv1:assembly>

否则,解决方法可能是有一个单独的“加载器”用户启动的应用程序,仅使用 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):

<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <assemblyIdentity version="1.0.0.0" name="HostsChanger" />
   <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
      <security>
         <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
            <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
         </requestedPrivileges>
      </security>
   </trustInfo>
</asmv1:assembly>

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 (so Process.StartInfo.Verb = "runas";).

行至春深 2024-09-15 19:15:30

我要在这里摸索一下,说这是一个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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文