Click here to Skip to main content
12,289,989 members (71,931 online)
Click here to Skip to main content
Add your own
alternative version


197 bookmarked

SwitchNetConfig - Laptop users, quickly switch network and proxy configuration in different places

, 6 May 2004 CPOL
Rate this:
Please Sign up or sign in to vote.
A handy utility for laptop users which stores network and proxy configuration as profiles and apply a profile very quickly whenever laptop goes to a different network


This is a tiny utility which stores multiple network and proxy configuration as profiles. You can apply a profile to set specific network setting and proxy from simple shortcuts.


If you have a laptop, when you go to different places, you have to change Network configuration and browser proxy. I have to go to 4 different places everyday with my laptop; my university, my offices and my home. Everywhere I have to change my IP setting and IE Proxy. I could write a WMI Script to do all of them. Unfortunately, for some reason WMI is not working from VB Script in my laptop but somehow it is working from .NET. So, I have made a handy utility which I can use to store configuration profiles for different places and apply them very quickly just by pressing one key and a ENTER at system startup. This program automatically sets IP, Subnet, Gateway and DNS for my LAN card and also changes the IE Proxy setting.

Using the code

You can setup in 3 easy steps.

  • 1. Click New and enter a profile name.
  • 2. Set IP configuration
  • 3. Set IE Proxy configuration

It’s even easier to use:

  • 1. Press the first letter of your profile. For example, “h” for Home.
  • 2. Press ENTER and wait as it applies the profile.
  • 3. Press ESC to quit.

Points of Interest

It’s a very simple Winforms application written in C#. The key technologies are:

  • 1. WMI
  • 2. IE Registry tweaking
  • 3. XML Serialization


Windows Management Intrumentation (WMI) helps you work with Network configuration. For example, the following code retrieves Network Setting for a given Network Card.

/// <summary>
/// Returns the network card configuration of the specified NIC
/// </summary>
/// <PARAM name="nicName">Name of the NIC</PARAM>
/// <PARAM name="ipAdresses">Array of IP</PARAM>
/// <PARAM name="subnets">Array of subnet masks</PARAM>
/// <PARAM name="gateways">Array of gateways</PARAM>
/// <PARAM name="dnses">Array of DNS IP</PARAM>
public static void GetIP( string nicName, out string [] ipAdresses, 
  out string [] subnets, out string [] gateways, out string [] dnses )
  ipAdresses = null;
  subnets = null;
  gateways = null;
  dnses = null;

  ManagementClass mc = new ManagementClass(
  ManagementObjectCollection moc = mc.GetInstances();

  foreach(ManagementObject mo in moc)
    // Make sure this is a IP enabled device. 
    // Not something like memory card or VM Ware
    if( mo["ipEnabled"] as bool )
      if( mo["Caption"].Equals( nicName ) )
        ipAdresses = (string[]) mo["IPAddress"];
        subnets = (string[]) mo["IPSubnet"];
        gateways = (string[]) mo["DefaultIPGateway"];
        dnses = (string[]) mo["DNSServerSearchOrder"];


“Win32_NetworkAdapterConfiguration” gives you the collection of network adapters installed. Be careful, all the entries in this list may not be your LAN card.

Similarly you can set Network configuration just by a couple of lines.

/// <summary>
/// Set IP for the specified network card name
/// </summary>
/// <PARAM name="nicName">Caption of the network card</PARAM>
/// <PARAM name="IpAddresses">Comma delimited string 
///           containing one or more IP</PARAM>
/// <PARAM name="SubnetMask">Subnet mask</PARAM>
/// <PARAM name="Gateway">Gateway IP</PARAM>
/// <PARAM name="DnsSearchOrder">Comma delimited DNS IP</PARAM>
public static void SetIP( string nicName, string IpAddresses, 
  string SubnetMask, string Gateway, string DnsSearchOrder)
  ManagementClass mc = new ManagementClass(
  ManagementObjectCollection moc = mc.GetInstances();

  foreach(ManagementObject mo in moc)
    // Make sure this is a IP enabled device. 
    // Not something like memory card or VM Ware
    if( mo["IPEnabled"] as bool )
      if( mo["Caption"].Equals( nicName ) )

        ManagementBaseObject newIP = 
          mo.GetMethodParameters( "EnableStatic" );
        ManagementBaseObject newGate = 
          mo.GetMethodParameters( "SetGateways" );
        ManagementBaseObject newDNS = 
          mo.GetMethodParameters( "SetDNSServerSearchOrder" );
        newGate[ "DefaultIPGateway" ] = new string[] { Gateway };
        newGate[ "GatewayCostMetric" ] = new int[] { 1 };

        newIP[ "IPAddress" ] = IpAddresses.Split( ',' );
        newIP[ "SubnetMask" ] = new string[] { SubnetMask };

        newDNS[ "DNSServerSearchOrder" ] = DnsSearchOrder.Split(',');

        ManagementBaseObject setIP = mo.InvokeMethod( 
          "EnableStatic", newIP, null);
        ManagementBaseObject setGateways = mo.InvokeMethod( 
          "SetGateways", newGate, null);
        ManagementBaseObject setDNS = mo.InvokeMethod( 
          "SetDNSServerSearchOrder", newDNS, null);


Internet Explorer Proxy

Changing Internet Explorer Proxy setting is a tricky job. There are 3 registry keys that you need to handle:

CURRENT_USER\ Software\Microsoft\Windows\CurrentVersion\Internet Settings

  • ProxyServer – string – Name of proxy server
  • ProxyEnable – integer – 1 for enabled, 0 for disabled
  • ProxyOverride – string – a list of hosts that you need to directly access without proxy

XML Serialization

XML Serialization makes it very easy to persist object graph in human readable form. For example, this is the XML that the program generates:

<?xml version="1.0"?>
<ConfigWrapper xmlns:xsd="" 
    <anyType xsi:type="Profile">
        <anyType xsi:type="NICProfile">
          <Name>[00000001] Intel(R) PRO/100 
             VE Network Connection</Name>
        <BypassAddresses />

It makes it very easy to modify the file manually. You can save an entire object graph just by writing 3 lines of code:

// Use XML Serializer to serialize the content of the specified array list
XmlSerializer serializer = new XmlSerializer( typeof( ConfigWrapper ) );
// open the profile file
StreamWriter writer = new StreamWriter( PROFILE_FILE_NAME, false );        
// Serialize the array list to the file
serializer.Serialize( writer.BaseStream, wrapper );


  • Ver 1.0 - Store multiple profiles, multiple NIC setting, IE Proxy
  • (Planning) Ver 2.0 - Execute batch file before and after profile setting


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


About the Author

Omar Al Zabir
Architect BT, UK (ex British Telecom)
United Kingdom United Kingdom

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
osueque15-Dec-12 2:16
memberosueque15-Dec-12 2:16 
QuestionUnable to change the IP Pin
devil13007811-Nov-12 18:36
memberdevil13007811-Nov-12 18:36 
GeneralMy vote of 4 Pin
rajeshhee26-Aug-12 18:42
memberrajeshhee26-Aug-12 18:42 
Questionhelp plz Pin
AmirAmirA6-Jun-12 20:57
memberAmirAmirA6-Jun-12 20:57 
QuestionNice try, but... Pin
Schmal1-Feb-12 17:56
memberSchmal1-Feb-12 17:56 
... (there is always a 'but'):
- Setting IP params always needs admin rights (XP and newer). Switching off UAC or even running your whole session as an admin is not an option. Think of bigger companies: Do you really want to grant local admin rights to all these users?
- Every IP address must respect its corresponding subnet mask, which will be different, depending the size of the subnet. You happen to be lucky as long as you're working on 24 bit subnets only, but as soon as it's no longer "" everywhere you will need to pair every IP address with its own subnet mask.

Thanks for sharing your programming experience but nowadays such a tool shouldn't be needed anymore. A properly configured DHCP server should be setting your NIC when connecting to a network, and IE settings should be propagated automatically as shown for example on [Internet Explorer Automatic Configuration].

No offence!
Questionmy vote is 4 Pin
ayongwust1-Nov-11 0:28
memberayongwust1-Nov-11 0:28 
QuestionProblem with the proxy Pin
patuco30-Sep-11 1:08
memberpatuco30-Sep-11 1:08 
GeneralMy vote of 5 Pin
Basarat Ali Syed28-Feb-11 19:37
memberBasarat Ali Syed28-Feb-11 19:37 
GeneralCommand Line Pin
TeerachaiJ6-Feb-11 19:43
memberTeerachaiJ6-Feb-11 19:43 
GeneralThank's Pin
Rodolfo Centeno Sicche14-Jan-11 6:59
memberRodolfo Centeno Sicche14-Jan-11 6:59 
GeneralWorked well with Win 7 prof. Pin
maqh19-Mar-10 7:15
membermaqh19-Mar-10 7:15 
GeneralTested with several proxy IP servers and works great! Pin
proxydev18-Mar-10 12:35
memberproxydev18-Mar-10 12:35 
GeneralLoad/Save method Pin
anen30-Jan-10 18:47
memberanen30-Jan-10 18:47 
GeneralRe: Load/Save method Pin
brk12330-Jun-10 2:06
memberbrk12330-Jun-10 2:06 
GeneralVery nice! Pin
quicoli1-Jul-09 1:48
memberquicoli1-Jul-09 1:48 
GeneralRe: Very nice! Pin
Omar Al Zabir1-Jul-09 6:03
mvpOmar Al Zabir1-Jul-09 6:03 
GeneralThanks! Pin
legolasgreenleaf3-Apr-09 1:51
memberlegolasgreenleaf3-Apr-09 1:51 
GeneralWell Buillt Utility Pin
Kannan.P12-Jan-09 23:37
memberKannan.P12-Jan-09 23:37 
GeneralSuggested update - Profile file location Pin
TimothyP14-Oct-08 20:42
memberTimothyP14-Oct-08 20:42 
GeneralSwitchNetConfig a super prog - THANKS Pin
pisoft22-May-08 10:56
memberpisoft22-May-08 10:56 
GeneralAmazing Pin
Jonathan C Dickinson13-May-08 20:36
memberJonathan C Dickinson13-May-08 20:36 
GeneralI guess you have still have lot more things to share.. Pin
javedahassan6-Feb-08 9:05
memberjavedahassan6-Feb-08 9:05 
GeneralVista Business Pin
Edcel Kahayon23-Jan-08 21:36
memberEdcel Kahayon23-Jan-08 21:36 
GeneralRe: Vista Business Pin
Nordin Rahman4-Mar-08 22:42
memberNordin Rahman4-Mar-08 22:42 
GeneralHello! Pin
adri68ro12-Dec-07 18:32
memberadri68ro12-Dec-07 18:32 
GeneralRe: Hello! Pin
George Thackray8-Oct-09 6:38
memberGeorge Thackray8-Oct-09 6:38 
oussamaghanem21-Oct-07 21:29
memberoussamaghanem21-Oct-07 21:29 
TimothyP14-Oct-08 20:38
memberTimothyP14-Oct-08 20:38 
Omar Al Zabir14-Oct-08 20:59
memberOmar Al Zabir14-Oct-08 20:59 
Generalimplementation Pin
eabidi775-Sep-07 11:06
membereabidi775-Sep-07 11:06 
GeneralRe: implementation Pin
Omar Al Zabir5-Sep-07 16:40
memberOmar Al Zabir5-Sep-07 16:40 
GeneralHelpful Pin
erwin satrya20-Aug-07 3:38
membererwin satrya20-Aug-07 3:38 
QuestionDHCP IP/Static DNS Pin
David Dreggors5-Feb-07 16:30
memberDavid Dreggors5-Feb-07 16:30 
GeneralProblem with multiple IP addresses Pin
p0rkskratchin25-Jan-07 22:59
memberp0rkskratchin25-Jan-07 22:59 
AnswerRe: Problem with multiple IP addresses Pin
Chris Tynes6-Feb-07 19:56
memberChris Tynes6-Feb-07 19:56 
GeneralLittle suggest Pin
Thomas.Mueller66-Sep-06 6:32
memberThomas.Mueller66-Sep-06 6:32 
GeneralConvert this to VB.NET Pin
mrodriguezc1-Jun-06 8:00
membermrodriguezc1-Jun-06 8:00 
GeneralApplication Error Pin
roundi21-May-06 1:34
memberroundi21-May-06 1:34 
GeneralWonderful Pin
tuca.ssa13-Mar-06 1:07
membertuca.ssa13-Mar-06 1:07 
GeneralSetIP only works for Gateway Pin
mystique200028-Sep-05 6:36
membermystique200028-Sep-05 6:36 
GeneralAlternative network setting causes problem Pin
Frank Esser24-Aug-05 23:50
memberFrank Esser24-Aug-05 23:50 
GeneralVB 6 Version Pin
Jeiel Borner22-Aug-05 8:31
memberJeiel Borner22-Aug-05 8:31 
QuestionCould u post a vc6 demo? Pin
Tcpip200521-Aug-05 22:45
memberTcpip200521-Aug-05 22:45 
AnswerRe: Could u post a vc6 demo? Pin
Tcpip200521-Aug-05 22:49
memberTcpip200521-Aug-05 22:49 
Generalgreat Pin
thomasFo17-Aug-05 23:39
memberthomasFo17-Aug-05 23:39 
Questiondoes this assume all networks have the same workgroup or domain? Pin
datamodel10-Aug-05 19:10
memberdatamodel10-Aug-05 19:10 
GeneralDisable network adapters Pin
Anonymous24-Jun-05 10:56
sussAnonymous24-Jun-05 10:56 
GeneralXML questions Pin
Tom Wright18-May-05 6:43
memberTom Wright18-May-05 6:43 
GeneralRe: XML questions Pin
Omar Al Zabir18-May-05 21:38
memberOmar Al Zabir18-May-05 21:38 
GeneralRe: XML questions Pin
Tom Wright19-May-05 11:56
memberTom Wright19-May-05 11:56 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160518.1 | Last Updated 7 May 2004
Article Copyright 2004 by Omar Al Zabir
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid