How can I programmatically edit the hosts file in Windows 7 / Server 2008?

I am writing a small WPF utility for managing entries in the hosts file for dev purposes. As you know, the hosts file is protected by newer operating systems (Win 7/2008 / Vista).

I added a manifest to my application to set requestExecutionLevel to "requireAdministrator", as described in detail here (using the "easy way" ") and in the corresponding question here .

Unfortunately, this did not work for me. When I launch the application, there is no prompt for promotion, and calling the File.AppendText file for the hosts file still throws a System.UnauthorizedAccessException: "Access to 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?

+7
security c # wpf elevated-privileges hosts-file
source share
3 answers

Paraphrased from my previous comment, turned into an answer:

Ho1's answer contains app.manifest, which is exactly the same as the application I'm working on and working on it. The difference here is that the file name is "app.manifest", and it is indicated by the "Manifest" project parameter (on the "Application" tab).

+3
source share

I'm not sure if this will matter, but your fragment of the manifest is slightly different from my understanding of how it should be (although it may 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, the job may be to have a separate β€œLoader” application that the user starts with and run your real WPF tool using Verb runas , as described in this blog post (like Process.StartInfo.Verb = "runas"; ).

+2
source share

I am going to hit in the dark here and say that this is a script signature issue. I have not heard you mention signing your statement. As far as I understand, unlike Vista, in Windows 2008/7 the only way to launch the application is elevated is to have a signed application manifest that defines the privilege level that the application requires. If you need help signing, here is an article on how to sign your application: http://msdn.microsoft.com/en-us/library/bb756995.aspx

+1
source share

All Articles