Click here to Skip to main content
15,860,861 members
Articles / Programming Languages / C#
Article

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 293.9K   6.9K   104   30
Configure TCP/IP Settings using WMI and C#

Introduction

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

C#
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("Printing Default Gateway Info:"); 
          Console.WriteLine(objMO["DefaultIPGateway"].ToString()); 
          
          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(sIP); 
 
          Console.WriteLine("Printing SubNet Info:"); 

          foreach(string sNet in subnets) 
               Console.WriteLine(sNet);
}

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

C#
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"); 
          


          //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); 


          
          Console.WriteLine(
             "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 http://msdn.microsoft.com/library/en-us/wmisdk/wmi/

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

 
QuestionLicense conditions Pin
thomas dechant21-May-18 8:59
thomas dechant21-May-18 8:59 
QuestionStatic IP C# Pin
Member 1050912721-Apr-16 6:56
Member 1050912721-Apr-16 6:56 
Questionabout static ip address Pin
Bhavya MM6-Sep-12 17:59
Bhavya MM6-Sep-12 17:59 
QuestionAbout static ip address Pin
Bhavya MM6-Sep-12 2:44
Bhavya MM6-Sep-12 2:44 
QuestionHow to set the default gateway ip to empty or none Pin
geykelraul25-Jul-11 9:10
geykelraul25-Jul-11 9:10 
AnswerRe: How to set the default gateway ip to empty or none Pin
Mauri Ketonen13-Nov-11 7:48
Mauri Ketonen13-Nov-11 7:48 
QuestionReturn code for ReleaseDHCPLeaseAll Pin
TAHAIC5-Jun-09 1:03
TAHAIC5-Jun-09 1:03 
Generalthanks Pin
Xmen Real 13-Mar-09 7:50
professional Xmen Real 13-Mar-09 7:50 
GeneralipEnabled Vista Pin
ceeyt_pp30-Oct-08 6:09
ceeyt_pp30-Oct-08 6:09 
GeneralRe: ipEnabled Vista Pin
Xmen Real 13-Mar-09 17:09
professional Xmen Real 13-Mar-09 17:09 
Generalsetting DNS as well Pin
sbini16-Jul-08 4:55
sbini16-Jul-08 4:55 
GeneralThanks very much ... Pin
funnyfrancois12-Jun-08 23:29
funnyfrancois12-Jun-08 23:29 
GeneralThanx Pin
Nitin S24-May-08 6:18
professionalNitin S24-May-08 6:18 
Generalbro help. i want to change ip if button click ip should change.i checked netsh but it disconnect net plz help Pin
Member 1247708623-Apr-16 1:35
Member 1247708623-Apr-16 1:35 
GeneralAwesome. Pin
remarkpk116-Jan-08 17:12
remarkpk116-Jan-08 17:12 
QuestionHow do you get the Default Gateway IP of a router? Pin
ebonnettit21-Jun-07 11:31
ebonnettit21-Jun-07 11:31 
Generalusing SetIP only works for Gateway Pin
mystique200024-Sep-05 12:03
mystique200024-Sep-05 12:03 
GeneralWoes on Win98 Pin
User 4864825-Apr-04 21:03
User 4864825-Apr-04 21:03 
GeneralRe: Woes on Win98 Pin
Anonymous21-Nov-04 22:38
Anonymous21-Nov-04 22:38 
GeneralRe: Woes on Win98 Pin
john anvet25-Jul-08 21:41
john anvet25-Jul-08 21:41 
QuestionDNS servers? Pin
Severedlimb8-Feb-04 8:42
Severedlimb8-Feb-04 8:42 
AnswerHOw to change the IP of a linix machine? Pin
Thulasi ramu J15-Apr-04 0:26
Thulasi ramu J15-Apr-04 0:26 
GeneralDifferent network adapters Pin
Alois Kraus5-Jan-04 2:47
Alois Kraus5-Jan-04 2:47 
GeneralRe: Different network adapters Pin
WaldenL10-Oct-05 5:21
WaldenL10-Oct-05 5:21 
GeneralRe: Different network adapters Pin
Roland_W25-Jan-06 21:39
Roland_W25-Jan-06 21:39 

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.