Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » Applications » Downloads
 
Add your own
alternative version

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

, 6 May 2004
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)

Share

About the Author


| Advertise | Privacy | Mobile
Web03 | 2.8.140814.1 | Last Updated 7 May 2004
Article Copyright 2004 by Omar Al Zabir
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid