Click here to Skip to main content
15,123,679 members
Articles / Desktop Programming / Windows Forms
Posted 19 Jan 2011

Tagged as


13 bookmarked

Host Switcher

Rate me:
Please Sign up or sign in to vote.
4.67/5 (5 votes)
19 Jan 2011CPOL1 min read
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

Daniel Carvalho Liedke
Brazil Brazil
I started development 31 years from now, since MSX basic. Started Windows programming with VB 2.0 and Web programming with ASP 3.0. Then I built Windows Forms, Web Applications, NT services and WPF applications using Microsoft.NET. I am MCP in Visual Basic 6.0, MCAD and MCSD.NET in Framework 1.1, MCPD Web in Framework 2.0, MCTS in .NET 3.5 workflow, MCTS in .NET 3.5 communication foundation, windows presentation foundation and MVC applications. Currently working with MVC Web Application and WCF services using Micro Services architecture proposed by me. Besides programming I love running, swimming, cats and movies.

Comments and Discussions

GeneralMight want to know about an earlier HostSwitcher Pin
msorens28-Jan-11 4:58
Membermsorens28-Jan-11 4:58 
GeneralRe: Might want to know about an earlier HostSwitcher Pin
Daniel Carvalho Liedke31-Jan-11 12:08
MemberDaniel Carvalho Liedke31-Jan-11 12:08 
Questionreboot ? Pin
kiquenet.com24-Jan-11 4:52
professionalkiquenet.com24-Jan-11 4:52 
AnswerRe: reboot ? Pin
lobotomy24-Jan-11 6:33
professionallobotomy24-Jan-11 6:33 
GeneralRe: reboot ? Pin
Daniel Carvalho Liedke25-Jan-11 0:47
MemberDaniel Carvalho Liedke25-Jan-11 0:47 
GeneralThank you Pin
KenJohnson19-Jan-11 20:17
MemberKenJohnson19-Jan-11 20:17 
Interesting and thanks for your effort. I wrote a few applications to do this sort of thing a while back and it brings back memories. If I may, I would suggest that you simply alter the permissions before a write to the hosts file and then return them to their previous state as far as permissions afterwards. The other suggestion that I would have is that you group the host entries in groups like 'prelaunch' and 'postlaunch' and allow people to swith them in groups. These are both pretty easy to impliment and would improve the app. If you want any assistance you can respond either privately or publicly and I would be glad to be of help. You can do really cool and fun things with host files.
Ken Johnson

Latest article WPF Color Picker Construction Kit

GeneralRe: Thank you [modified] Pin
Daniel Carvalho Liedke20-Jan-11 1:18
MemberDaniel Carvalho Liedke20-Jan-11 1:18 
GeneralRe: Thank you Pin
Daniel Carvalho Liedke20-Jan-11 2:39
MemberDaniel Carvalho Liedke20-Jan-11 2:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.