Wednesday 18 May 2011

IPSwitcher: Quickly switch IPAddress Configuration

Are you like me and use different firewalls and configurations depending on what you are doing,
find it irritating to go through the process of changing your IP, gateway or DNS.
Well I put this tool together to help

It sits in the tray Icon, just right click and select your configuration.
Or enter the configuration where you can make changes,
or configure it directly in the config file:

   1: <string>-Name Gaming -AdapterName RTL8167 -IpAddress 10.0.0.65 -Mask 255.255.255.0 -Gateway 10.0.0.25 -DNS 10.0.0.20</string>
   2: <string>-Name Browsing -AdapterName RTL8167 -IpAddress 10.0.0.65 -Mask 255.255.255.0 -Gateway 10.0.0.20 -DNS 10.0.0.20</string>
   3:  

image





image





image



How I built it:
You need System.Management

 C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Management.dll

Here is how to get a list of adapters on the machine:

   1: public IEnumerable<dynamic> GetAdapters()
   2:         {
   3:             ManagementClass objMC = new ManagementClass(
   4:                 "Win32_NetworkAdapterConfiguration");
   5:             ManagementObjectCollection objMOC = objMC.GetInstances();
   6:             foreach (dynamic objMO in objMOC)
   7:             {
   8:  
   9:                 if (objMO["IPEnabled"])
  10:                 {
  11:                     yield return new Adapter(objMO,
  12:                         objMO["Caption"].ToString(),
  13:                         objMO["ServiceName"].ToString());
  14:                 }
  15:             }
  16:         }

 Then update the adapter:

   1: internal void UpdateAdapter()
   2:         {
   3:             foreach (var adapter in GetAdapters())
   4:             {
   5:                 if (adapter.Name.CompareTo(_adapterName) == 0)
   6:                 {
   7:  
   8:                     ManagementBaseObject objNewIP = null;
   9:                     ManagementBaseObject objSetIP = null;
  10:                     ManagementBaseObject objNewGate = null;
  11:                     objNewIP = adapter.ManagementObject.GetMethodParameters("EnableStatic");
  12:                     objNewGate = adapter.ManagementObject.GetMethodParameters("SetGateways");
  13:  
  14:                     objNewGate["DefaultIPGateway"] = new string[] { _Gateway };
  15:                     objNewGate["GatewayCostMetric"] = new int[] { 1 };
  16:                     //Set IPAddress and Subnet Mask
  17:  
  18:                     objNewIP["IPAddress"] = new string[] { _IpAddress };
  19:                     objNewIP["SubnetMask"] = new string[] { _mask };
  20:  
  21:                     objSetIP = adapter.ManagementObject.InvokeMethod("EnableStatic", objNewIP, null);
  22:                     objSetIP = adapter.ManagementObject.InvokeMethod("SetGateways", objNewGate, null);
  23:                       
  24:                 }
  25:             }
  26:         }

The complete project with source is available here:

No comments:

Post a Comment