Click here to Skip to main content
11,494,964 members (59,671 online)
Click here to Skip to main content

Configuring TCP/IP Settings using WMI and C#

, 22 Dec 2003 209.7K 4.7K 98
Rate this:
Please Sign up or sign in to vote.
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

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.

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

Share

About the Author

Logu Krishnan
Web Developer
India India
No Biography provided

Comments and Discussions

 
Questionabout static ip address Pin
Bhavya MM6-Sep-12 18:59
memberBhavya MM6-Sep-12 18:59 
QuestionAbout static ip address Pin
Bhavya MM6-Sep-12 3:44
memberBhavya MM6-Sep-12 3:44 
QuestionHow to set the default gateway ip to empty or none Pin
geykelraul25-Jul-11 10:10
membergeykelraul25-Jul-11 10:10 
AnswerRe: How to set the default gateway ip to empty or none Pin
Mauri Ketonen13-Nov-11 8:48
memberMauri Ketonen13-Nov-11 8:48 
QuestionReturn code for ReleaseDHCPLeaseAll Pin
TAHAIC5-Jun-09 2:03
memberTAHAIC5-Jun-09 2:03 
Generalthanks Pin
Xmen 13-Mar-09 8:50
member Xmen 13-Mar-09 8:50 
GeneralipEnabled Vista Pin
ceeyt_pp30-Oct-08 7:09
memberceeyt_pp30-Oct-08 7:09 
GeneralRe: ipEnabled Vista Pin
Xmen 13-Mar-09 18:09
member Xmen 13-Mar-09 18:09 
Generalsetting DNS as well Pin
sbini16-Jul-08 5:55
membersbini16-Jul-08 5:55 
GeneralThanks very much ... Pin
funnyfrancois13-Jun-08 0:29
memberfunnyfrancois13-Jun-08 0:29 
GeneralThanx Pin
Nitin Sawant24-May-08 7:18
memberNitin Sawant24-May-08 7:18 
GeneralAwesome. Pin
remarkpk116-Jan-08 18:12
memberremarkpk116-Jan-08 18:12 
QuestionHow do you get the Default Gateway IP of a router? Pin
ebonnettit21-Jun-07 12:31
memberebonnettit21-Jun-07 12:31 
I want to get the external facing Default Gateway IP of my linksys router. Anybody know how? Is it possible? I tried using NATUPNPLib but I keep getting 0.0.0.0

Any help would be really appreciated.

Thanks
Generalusing SetIP only works for Gateway Pin
mystique200024-Sep-05 13:03
membermystique200024-Sep-05 13:03 
GeneralWoes on Win98 Pin
Magnus Persson25-Apr-04 22:03
memberMagnus Persson25-Apr-04 22:03 
GeneralRe: Woes on Win98 Pin
Anonymous21-Nov-04 23:38
sussAnonymous21-Nov-04 23:38 
GeneralRe: Woes on Win98 Pin
john anvet25-Jul-08 22:41
memberjohn anvet25-Jul-08 22:41 
QuestionDNS servers? Pin
Severedlimb8-Feb-04 9:42
memberSeveredlimb8-Feb-04 9:42 
AnswerHOw to change the IP of a linix machine? Pin
Thulasi ramu J15-Apr-04 1:26
memberThulasi ramu J15-Apr-04 1:26 
GeneralDifferent network adapters Pin
Alois Kraus5-Jan-04 3:47
memberAlois Kraus5-Jan-04 3:47 
GeneralRe: Different network adapters Pin
WaldenL10-Oct-05 6:21
memberWaldenL10-Oct-05 6:21 
GeneralRe: Different network adapters Pin
Roland_W25-Jan-06 22:39
memberRoland_W25-Jan-06 22:39 
Generalcorrect msdn link Pin
Daniel Fisher (lennybacon)30-Dec-03 0:34
memberDaniel Fisher (lennybacon)30-Dec-03 0:34 
GeneralRe: correct msdn link Pin
Xmen 13-Mar-09 17:18
member Xmen 13-Mar-09 17:18 
QuestionHow do I to Control NetworkAdapter state (enable or disenable ) in c#? Pin
moonlight21cn200124-Dec-03 20:12
membermoonlight21cn200124-Dec-03 20:12 
AnswerRe: How do I to Control NetworkAdapter state (enable or disenable ) in c#? Pin
uwgmxbscdbuwgmxbscdb6-Jul-06 6:55
memberuwgmxbscdbuwgmxbscdb6-Jul-06 6:55 
GeneralRe: How do I to Control NetworkAdapter state (enable or disenable ) in c#? Pin
--Simas--27-Apr-07 8:39
member--Simas--27-Apr-07 8:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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