Click here to Skip to main content
Click here to Skip to main content

Host Switcher

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

HostSwitcher.png

Introduction

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 - DITSERVER01.domain.com
#10.65.116.200    site.domain.com

# SIT - SITSERVER01.domain.com
10.59.169.44    site.domain.com

# Prod - PRODSERVER01.domain.com
#10.59.71.132     site.domain.com

# Prod - PRODSERVER02.domain.com
#10.59.71.133     site.domain.com

# Prod - PRODSERVER03.domain.com
#10.59.61.169     site.domain.com

# Prod - PRODSERVER04.domain.com
#10.59.61.220     site.domain.com

In this example, when the user selects the option "DIT - DITSERVER01.domain.com" 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.

History

  • Initial release 1.0.

License

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

Share

About the Author

Daniel Carvalho Liedke
Software Developer (Senior)
Brazil Brazil
I started development 22 years from now, since MSX basic. Started Windows programming with VB 2.0 and Web programming with ASP 3.0. Now I build 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 and windows presentation foundation. Besides programming I love surfing.

Comments and Discussions

 
GeneralMight want to know about an earlier HostSwitcher Pinmembermsorens28-Jan-11 3:58 
GeneralRe: Might want to know about an earlier HostSwitcher PinmemberDaniel Carvalho Liedke31-Jan-11 11:08 
Questionreboot ? Pinmemberalhambra-eidos24-Jan-11 3:52 
AnswerRe: reboot ? Pinmemberlobotomy24-Jan-11 5:33 
GeneralRe: reboot ? PinmemberDaniel Carvalho Liedke24-Jan-11 23:47 
If using Internet Explorer or any other browser the internet browser needs to be restarted when hosts changes in order to get new hosts configuration. Thank you!
GeneralThank you PinmemberKenJohnson19-Jan-11 19:17 
GeneralRe: Thank you [modified] PinmemberDaniel Carvalho Liedke20-Jan-11 0:18 
GeneralRe: Thank you PinmemberDaniel Carvalho Liedke20-Jan-11 1:39 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140814.1 | Last Updated 19 Jan 2011
Article Copyright 2011 by Daniel Carvalho Liedke
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid