Programming out of need – HOSTS File Editor

I work within the realm of QA testing and have to constantly edit my Windows hosts file to redirect to other servers during the course of an average day and although I had a few batch files to run I wished I had a tool that I could use to do it quicker. I looked around the net and didn’t find anything useful, surprisingly.

I thought about it for a few weeks and then just decided to dive in and take a stab at it so at home on my home PC, because I don’t want any conflicts doing this on a work machine, I started learning C# in Visual Studio. All I was armed with really was a plain text file with redirects that I could copy and paste into the hosts file depending on what I needed to accomplish test wise and a couple batch files I used to previously do it.

Example:

##=========WORDPRESS========
## redirect to wp0 on domain
#111.111.111.111 www.domain.com secure.domain.com

##===========VARN===========
## redirect to v0 on domain
#111.111.111.111 v0.ourdomain.com

## redirect to vs on domain
#111.111.111.111 www.domain.com secure.domain.com

##======== TEST SERVER ========
## redirect to test on domain
#111.111.11.11 test.domain.com

At first I thought of doing an app where I could select each server from a combobox and then launch Notepad as Admin to copy and then save the info, then it was a combobox with form button events and lengthy if else if statement to write the server info to the hosts file. It was certainly worse code for wear for sure.

I took a step back from that to reanalyze my need and through several iterations came up with a host file manager that worked for me. In a nutshell it will read in my hosts data file (hostsdata.txt) into a checkedbox component, allowing me to check what I want and then write those items to my systems hosts file.

I made some additions to the tool such as starting with elevated admin privileges which are needed in order to write to the hosts file, the ability to reset the hosts file to default commented text, a way to perform functions similar to ipconfig /release, /renew and /flushdns using WMI and added the small editor t
o the tool to allow me to edit the hostsdata.txt file for new or changed redirect information.

Main Application

Main Application

As you can see in the screenshots, my data file entries have the hash tags which the hosts file recognizes as commenting. The procedure / block of code for writing the information to the hosts file will strip that forward hash tag (the #) so that it is written to file as un-commented already.

However, as you can tell in the screenshot of the main application, there are some checkboxes that do not contain a display value and I haven’t added any code for processing that so I just catch the exception to get on with my day – I just don’t click on the blank entries.

In any regard, I am not a professional programmer and essentially just dove in on this because I had a need and hated the way I currently had to deal with modifying the hosts file. I didn’t find any tools online so I created this app to use. I still tweak and refine this tool a little but take it as a tool that was just something whipped up to get ‘er done. Any comments are welcomed here or to my email or if you have trouble downloading the file for some reason – email at stevegossett[[at]]outlook.com.

It requires at a minimum .NET Framework version 4.5 to be installed. If you get an error when trying to write changes to the hosts file this will be why.

Host Editor Tool Download

So in contrast to my hosts editor app, below is the contents of a batch file to select from a list of IPs. I was using this before creating the hosts manager tool. Every once in awhile I still use this but only when I am on a system that barks at starting apps from a thumbdrive. Don’t forget to right-click and Run as…. Admin.

[code language=”text”]
@echo off
TITLE Modifying your HOSTS file
COLOR F0
ECHO.
:LOOP
SET Choice=
ECHO a = server1
ECHO b = server3
ECHO c = server4
ECHO d = stage
ECHO e = devserver
SET /P Choice="Point HOSTS to? Enter number or R to reset. (0-7,a-e,R)"
IF NOT ‘%Choice%’==” SET Choice=%Choice:~0,1%</code>

ECHO.
IF /I ‘%Choice%’==’0’ GOTO 0
IF /I ‘%Choice%’==’1’ GOTO 1
IF /I ‘%Choice%’==’2’ GOTO 2
IF /I ‘%Choice%’==’3’ GOTO 3
IF /I ‘%Choice%’==’5’ GOTO 5
IF /I ‘%Choice%’==’7’ GOTO 7
IF /I ‘%Choice%’==’a’ GOTO 8
IF /I ‘%Choice%’==’b’ GOTO 9
IF /I ‘%Choice%’==’c’ GOTO 10
IF /I ‘%Choice%’==’d’ GOTO 11
IF /I ‘%Choice%’==’e’ GOTO 12
IF /I ‘%Choice%’==’R’ GOTO RESET
ECHO.
GOTO Loop

:RESET
set hosts=%windir%system32driversetchosts
If exist %hosts% (
del /q %hosts%)
ECHO Carrying out requested modifications to your HOSTS file
ECHO #Empty hosts file&gt;&gt;%hosts%
GOTO END

:0
set hosts=%windir%system32driversetchosts
If exist %hosts% (
del /q %hosts%)
ECHO Carrying out requested modifications to your HOSTS file
ECHO 111.111.11.11 serv.domain.com www.domain.com domain.com secure.domain.com&gt;&gt;%hosts%
ECHO Finished
GOTO END

:1
set hosts=%windir%system32driversetchosts
If exist %hosts% (
del /q %hosts%)
ECHO Carrying out requested modifications to your HOSTS file
ECHO 111.111.11.11 serv.domain.com www.domain.com domain.com secure.domain.com&gt;&gt;%hosts%
ECHO Finished
GOTO END

:2
set hosts=%windir%system32driversetchosts
If exist %hosts% (
del /q %hosts%)
ECHO Carrying out requested modifications to your HOSTS file
ECHO 111.111.11.11 serv.domain.com www.domain.com domain.com secure.domain.com&gt;&gt;%hosts%
ECHO Finished
GOTO END

:3
set hosts=%windir%system32driversetchosts
If exist %hosts% (
del /q %hosts%)
ECHO Carrying out requested modifications to your HOSTS file
ECHO 111.111.11.11 serv.domain.com www.domain.com domain.com secure.domain.com&gt;&gt;%hosts%
ECHO Finished
GOTO END

:5
set hosts=%windir%system32driversetchosts
If exist %hosts% (
del /q %hosts%)
ECHO Carrying out requested modifications to your HOSTS file
ECHO 111.111.11.11 serv.domain.com www.domain.com domain.com secure.domain.com&gt;&gt;%hosts%
ECHO 222.222.22.22 dbcluster3&gt;&gt;%hosts%
ECHO Finished
GOTO END

:7
set hosts=%windir%system32driversetchosts
If exist %hosts% (
del /q %hosts%)
ECHO Carrying out requested modifications to your HOSTS file
ECHO 111.111.11.11 serv.domain.com www.domain.com domain.com secure.domain.com&gt;&gt;%hosts%
ECHO Finished
GOTO END

:8
REM server1.domain.com
set hosts=%windir%system32driversetchosts
If exist %hosts% (
del /q %hosts%)
ECHO Carrying out requested modifications to your HOSTS file
ECHO 111.111.11.11 domain.com www.domain.com&gt;&gt;%hosts%
ECHO Finished
GOTO END

:9
REM server3.domain.com
set hosts=%windir%system32driversetchosts
If exist %hosts% (
del /q %hosts%)
ECHO Carrying out requested modifications to your HOSTS file
ECHO 111.111.11.11 domain.com www.domain.com&gt;&gt;%hosts%
ECHO Finished
GOTO END

:10
REM sever4.domain.com
set hosts=%windir%system32driversetchosts
If exist %hosts% (
del /q %hosts%)
ECHO Carrying out requested modifications to your HOSTS file
ECHO 111.111.11.11 domain.com www.domain.com&gt;&gt;%hosts%
ECHO Finished
GOTO END

:11
REM stage.domain.com
set hosts=%windir%system32driversetchosts
If exist %hosts% (
del /q %hosts%)
ECHO Carrying out requested modifications to your HOSTS file
ECHO 111.111.11.11 domain.com www.domain.com&gt;&gt;%hosts%
ECHO Finished
GOTO END

:12
REM devserver.domain.com
set hosts=%windir%system32driversetchosts
If exist %hosts% (
del /q %hosts%)
ECHO Carrying out requested modifications to your HOSTS file
ECHO 111.111.11.11 domain.com www.domain.com devserver.domain.com&gt;&gt;%hosts%
ECHO Finished
GOTO END
:END
ECHO.
EXIT
[/code]