Click here to Skip to main content
977 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


Posted 25 Nov 2011
Licenced CPOL

Host Switcher

, 25 Nov 2011
Rate this:
Please Sign up or sign in to vote.
Quickly and easily alternate between servers in the hosts file.



Host Switcher parses the host file, shows the available options, and lets you set a host using the tray. This is useful when we point to a single DNS name for WCF, Web Services, and so on, but we can test different environments.

Using the Code

Important: Before running the utility, grant full control access to your user for the hosts file at %systemroot%\System32\drivers\etc\.

When running the application, it will parse the hosts file and build a menu with options in the tray. Here is an example of the hosts file:

# DIT -

# SIT -

# Prod -

# Prod -

# Prod -

# Prod -

In this example, when the user selects the option "DIT -" in the tray, the application will comment the SIT entry and uncomment the DIT entry. The "[No Hosts]" option will comment out all entries. The "View Hosts File" option will open the hosts file in Notepad for editing. If the hosts file is changed externally, the application will automatically rebuild the menu.

If right clicking the server option in the tray, it will copy the server name. When exiting the application, it will ask if the user wants to keep the current host configuration or comment all entries.

The code is simple, with the most important methods being ParseHostsFile() that will retrieve the entries in the hosts file and present them to the user, and UpdateHosts() that will comment the previous entry and uncomment the selected one.

Points of Interest

Nothing complex in the code, I just used FileSystemWatcher to get any external changes in the hosts file and automatically update the options for the application.


  • Initial release 1.0.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Sean Ewington
Technical Writer CodeProject
Canada Canada
Sean Ewington is the Content Manager for CodeProject.

His background in programming is primarily C++ and HTML, but has experience in other, "unsavoury" languages.

He loves movies, and likes to say inconceivable often, even if it does not mean what he thinks it means.

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
RA-Web1 | 2.8.181113.3 | Last Updated 25 Nov 2011
Article Copyright 2011 by Sean Ewington
Everything else Copyright © RootAdmin, 1999-2018
Layout: fixed | fluid