Switch Location – Make your hosts file location-aware

I run a few different services from my home network on local IPs.  When I take my laptop elsewhere – to Starbucks for example, those services no longer work because I’ve had to add their local IP addresses to my hosts file.

E.g.  dev.blakepics.com

When I take my laptop outside of the network, I want that IP to be removed from my hosts file, so it finds the right public IP address.  There are a few different ways of doing this, including:

  • Run a local network DNS server like named and configure those addresses locally.  This work pretty well, but adding another point of failure to my small network wasn’t ideal.
  • Manually edit your hosts file each time (C:\Windows\System32\drivers\etc\hosts).

I Googled around, and came across Mobile Net Switcher which will do exactly what I need – and switch a whole bunch of settings based on my current network.  It also has a huge amount of features I don’t need and costs €29 for the nag-free version.

It’s a perfectly price reason for an app like this, but I thought back to a quote from a friend of mine a few days ago: “The joys of being a programmer: when I want a program, I don’t have to pirate it. I just make it myself.”

So I wrote my own.

Download Switch Location here

It’s a far more limited version, and only supports switching your hosts file, but it does the job for what I need right now.  One of the features required which I hadn’t used before is some tasks requiring elevated permissions via UAC.

Adding a UAC shielded button was surprisingly simple.  Then it is just a case of starting a new process with elevated permissions:

[codesyntax lang=”csharp”]var proc = new ProcessStartInfo
UseShellExecute = true,
WorkingDirectory = Application.StartupPath,
FileName = Application.ExecutablePath,
Arguments = commandArguments,
Verb = “runas”

Then just detect the commandArguments parameter on the main application start and run your elevated tasks as necessary.

Once I’ve tidied up the source a little bit (it needs some Resharper loving), I’ll release the source code of the whole app here – in the meantime, you can download Switch Location here.

Leave a Reply?