Click here to Skip to main content
15,896,063 members
Articles / Programming Languages / C#

Configuring TCP/IP Settings using WMI and C#

Rate me:
Please Sign up or sign in to vote.
4.35/5 (28 votes)
22 Dec 2003 295.2K   6.9K   104  
Configure TCP/IP Settings using WMI and C#
using System;
using System.Management;

namespace TcpIPWMI
{
	
	public class TcpIPWMI
	{

		public void setIP(string IPAddress,string SubnetMask, string Gateway)
		{
			ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
			ManagementObjectCollection objMOC = objMC.GetInstances();

			foreach(ManagementObject objMO in objMOC)
			{
				if (!(bool) objMO["IPEnabled"])
					continue;

				try
				{
					ManagementBaseObject objNewIP = null;
					ManagementBaseObject objSetIP = null;
					ManagementBaseObject objNewGate = null;
					objNewIP = objMO.GetMethodParameters("EnableStatic");
					objNewGate = objMO.GetMethodParameters("SetGateways");
									
					objNewGate["DefaultIPGateway"] = new string[] {Gateway};
					objNewGate["GatewayCostMetric"] = new int[] {1};
					objNewIP["IPAddress"] = new string[] {IPAddress};
					objNewIP["SubnetMask"] = new string[] {SubnetMask};
					objSetIP = objMO.InvokeMethod("EnableStatic",objNewIP,null);
					objSetIP = objMO.InvokeMethod("SetGateways",objNewGate,null);
					
					Console.WriteLine("Updated IPAddress, SubnetMask and Default Gateway!");
				}
				catch(Exception ex)
				{
					Console.WriteLine("Unable to Set IP : " + ex.Message);

				}

			}
		}

		public void ListIP()
		{
			ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
			ManagementObjectCollection objMOC = objMC.GetInstances();

			foreach(ManagementObject objMO in objMOC)
			{
				if(!(bool)objMO["ipEnabled"])
					continue;

				Console.WriteLine(objMO["Caption"] + "," + objMO["ServiceName"] + "," + objMO["MACAddress"]) ;
				string[] ipaddresses = (string[]) objMO["IPAddress"];
				string[] subnets = (string[]) objMO["IPSubnet"];
				string[] gateways = (string[]) objMO["DefaultIPGateway"];

				Console.WriteLine(objMO["DefaultIPGateway"].ToString());

				Console.WriteLine("IPGateway");
				foreach(string sGate in gateways)
					Console.WriteLine (sGate);

				
				Console.WriteLine("Ipaddress");
				foreach(string sIP in ipaddresses)
					Console.WriteLine(sIP);
				
				Console.WriteLine("SubNet");
				foreach(string sNet in subnets)
					Console.WriteLine(sNet);

			}

		}

		public TcpIPWMI()
		{
			
		}


	}
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions