Click here to Skip to main content
13,344,281 members (59,264 online)
Click here to Skip to main content
Add your own
alternative version


102 bookmarked
Posted 22 Dec 2003

Configuring TCP/IP Settings using WMI and C#

, 22 Dec 2003
Rate this:
Please Sign up or sign in to vote.
Configure TCP/IP Settings using WMI and C#


This article demonstrates the power of WMI, on how to configure TCP/IP Setting programmatically using C#. This article is targeted at intermediate developers.

Using the code

WMI Extends the possibilities of .NET and simplifies the life while working on NetworkAdapters. The Following Code snippet lists all the Network adapters along with the IP Address, Subnet Mask, Default Gateway

public void ListIP() 
 ManagementClass objMC = new ManagementClass(
 ManagementObjectCollection objMOC = objMC.GetInstances(); 
foreach(ManagementObject objMO in objMOC) 

          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("Printing Default Gateway Info:"); 
          Console.WriteLine("Printing IPGateway Info:"); 
          foreach(string sGate in gateways) 
               Console.WriteLine (sGate); 

          Console.WriteLine("Printing Ipaddress Info:"); 

          foreach(string sIP in ipaddresses) 
          Console.WriteLine("Printing SubNet Info:"); 

          foreach(string sNet in subnets) 

Now, here is the code to configure TCP/IP Settings using WMI.

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

foreach(ManagementObject objMO in objMOC) 

      if (!(bool) objMO["IPEnabled"]) 

          ManagementBaseObject objNewIP = null; 
          ManagementBaseObject objSetIP = null; 
          ManagementBaseObject objNewGate = null; 

          objNewIP = objMO.GetMethodParameters("EnableStatic"); 
          objNewGate = objMO.GetMethodParameters("SetGateways"); 

          //Set DefaultGateway
          objNewGate["DefaultIPGateway"] = new string[] {Gateway}; 
          objNewGate["GatewayCostMetric"] = new int[] {1}; 

          //Set IPAddress and Subnet Mask
          objNewIP["IPAddress"] = new string[] {IPAddress}; 
          objNewIP["SubnetMask"] = new string[] {SubnetMask}; 
          objSetIP = objMO.InvokeMethod("EnableStatic",objNewIP,null); 
          objSetIP = objMO.InvokeMethod("SetGateways",objNewGate,null); 

             "Updated IPAddress, SubnetMask and Default Gateway!"); 

        catch(Exception ex) 
              MessageBox.Show("Unable to Set IP : " + ex.Message); } 

There are some interesting methods in the Win32_NetworkAdapterConfiguration WMI Class, which represents the behaviour and attributes of a NetworkAdapter, explore them at


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


About the Author

Logu Krishnan
Web Developer
India India
No Biography provided

You may also be interested in...


Comments and Discussions

Generalsetting DNS as well Pin
sbini16-Jul-08 5:55
membersbini16-Jul-08 5:55 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 23 Dec 2003
Article Copyright 2003 by Logu Krishnan
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid