Click here to Skip to main content
15,886,199 members
Articles / Programming Languages / C#

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

Rate me:
Please Sign up or sign in to vote.
4.66/5 (75 votes)
6 May 2004CPOL2 min read 558.2K   22.1K   199  
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
using System;
using System.Collections;
using System.Management;

namespace SwitchNetConfig
{
	/// <summary>
	/// A Helper class which provides convenient methods to set/get network
	/// configuration
	/// </summary>
	public class WMIHelper
	{
		#region Public Static

		/// <summary>
		/// Enable DHCP on the NIC
		/// </summary>
		/// <param name="nicName">Name of the NIC</param>
		public static void SetDHCP( string nicName )
		{
			ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
			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( (bool)mo["IPEnabled"] )
				{
					if( mo["Caption"].Equals( nicName ) )
					{
						ManagementBaseObject newDNS = mo.GetMethodParameters( "SetDNSServerSearchOrder" );
						newDNS[ "DNSServerSearchOrder" ] = null;
						ManagementBaseObject enableDHCP = mo.InvokeMethod( "EnableDHCP", null, null);
						ManagementBaseObject setDNS = mo.InvokeMethod( "SetDNSServerSearchOrder", newDNS, null);
					}
				}
			}
		}
		
		/// <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("Win32_NetworkAdapterConfiguration");
			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( (bool)mo["IPEnabled"] )
				{
					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);

						break;
					}
				}
			}
		}

		/// <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("Win32_NetworkAdapterConfiguration");
			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( (bool)mo["ipEnabled"] )
				{
					if( mo["Caption"].Equals( nicName ) )
					{
						ipAdresses = (string[]) mo["IPAddress"];
						subnets = (string[]) mo["IPSubnet"];
						gateways = (string[]) mo["DefaultIPGateway"];
						dnses = (string[]) mo["DNSServerSearchOrder"];

						break;
					}
				}
			}
		}

		/// <summary>
		/// Returns the list of Network Interfaces installed
		/// </summary>
		/// <returns>Array list of string</returns>
		public static ArrayList GetNICNames()
		{
			ArrayList nicNames = new ArrayList();

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

			foreach(ManagementObject mo in moc)
			{
				if((bool)mo["ipEnabled"])
				{
					nicNames.Add( mo["Caption"] );
				}
			}

			return nicNames;
		}

		#endregion
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Architect BT, UK (ex British Telecom)
United Kingdom United Kingdom

Comments and Discussions