How to change the hosts file in a Windows program?

how does a program in a C ++ / C / C # program change the contents of host files in windows? I know this sounds like phishing, to be honest.

+8
c ++ c c # windows hosts-file
source share
6 answers

First, you must request for administrative permission of the user. You can do this through your program class in your application. The code below asks the user for administrative access, then the user has the option to allow or deny it. If they deny this, this example does not launch the application.

As soon as the application is launched in administrative mode, its plain text with simple formatting. You don’t even need all the Microsoft comments included in the file, and a simple line parsing would be very good. The MSFT notes in the HOSTS file are all the necessary documentation since the HOSTS file itself is coming.

namespace Setup { using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using Setup.Forms; using System.Security.Principal; using System.Diagnostics; static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); WindowsPrincipal principal = new WindowsPrincipal(WindowsIdentity.GetCurrent()); bool administrativeMode = principal.IsInRole(WindowsBuiltInRole.Administrator); if (!administrativeMode) { ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.Verb = "runas"; startInfo.FileName = Application.ExecutablePath; try { Process.Start(startInfo); } catch { return; } return; } Application.Run(new ShellForm()); } } } 
+11
source share

The hosts file has a very simple format where each line can contain ip host entries

All you need is regular file uploads:

 using (StreamWriter w = File.AppendText(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "drivers/etc/hosts"))) { w.WriteLine("123.123.123.123 FQDN"); } 

Beware that by default you will need elevated privileges to write to the hosts file ...

To go back, it is better to take a backup copy of the file and restore it as soon as you are done.

+10
source share

The file is usually located in C:\Windows\System32\drivers\etc\hosts . Instead of hard coding the part of C:\Windows , you should use Environment.GetEnvironmentVariable("SystemRoot") to safely determine the root directory of the system.

Otherwise, you can write to it, like any other file, provided that you have the appropriate permissions.

+8
source share

The hosts file is just text. The format of each line contains the IP address and host name to which IP should be allowed, separated by a space. # stands for comment.

Example:

 # This is a comment- 127.0.0.1 mysuperhost.com 

The file is located here: C:\Windows\system32\drivers\etc\hosts . You (with good reason) must have administrator rights to write to him.

+4
source share

The most accurate way to find the location of the HOSTS file is to read the registry key HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\DataBasePath and add hosts to the end.

This will always point to the correct location for the current computer configuration and will work on all platforms running Windows NT with Windows NT 4.0.

+3
source share

As the guy who struggled with this problem easily came out, copy the hosts file to the temp folder, change it and copy it back using overwrite. Running the application as an administrator would be best.

0
source share

All Articles