HostSwitcher.png

Introduction

Host Switcher parses the host file, show available options and let 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 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. An example of 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 user selects option "DIT - DITSERVER01.domain.com" in the tray, the application will comment the SIT entry and uncomment the DIT entry.

Option "[No Hosts]" will comment out all entries.

Option "View Hosts File" will open hosts file in notepad for editing.

If hosts file is changed externally, 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 user wants to keep current host configuration or comment all entries.

The code is simple, with most important methods ParseHostsFile() that will retrieve the entries in hosts file and present to the user and UpdateHosts() that will comment 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 host file and automatically update the options for the application.

History

Initial release 1.0 

推荐.NET配套的通用数据层ORM框架:CYQ.Data 通用数据层框架