Click here to Skip to main content
Licence 
First Posted 22 Dec 2003
Views 162,168
Bookmarked 86 times

Configuring TCP/IP Settings using WMI and C#

By | 22 Dec 2003 | Article
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

About the Author

Logu Krishnan

Web Developer

India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionHow to set the default gateway ip to empty or none Pinmembergeykelraul9:10 25 Jul '11  
AnswerRe: How to set the default gateway ip to empty or none PinmemberMauri Ketonen7:48 13 Nov '11  
QuestionReturn code for ReleaseDHCPLeaseAll PinmemberTAHAIC1:03 5 Jun '09  
Generalthanks Pinmember Xmen 7:50 13 Mar '09  
GeneralipEnabled Vista Pinmemberceeyt_pp6:09 30 Oct '08  
GeneralRe: ipEnabled Vista Pinmember Xmen 17:09 13 Mar '09  
Generalsetting DNS as well Pinmembersbini4:55 16 Jul '08  
just in case someone wants to set a DNS as well ...
 
ManagementBaseObject objNewDNS = null;
objNewDNS = objMO.GetMethodParameters("SetDNSServerSearchOrder");
objNewDNS["DNSServerSearchOrder"] = new string[] { "1.2.3.4" };
objSetIP = objMO.InvokeMethod("SetDNSServerSearchOrder", objNewDNS, null);
 
cheers!
GeneralThanks very much ... Pinmemberfunnyfrancois23:29 12 Jun '08  
GeneralThanx PinmemberNitin Sawant6:18 24 May '08  
GeneralAwesome. Pinmemberremarkpk1117:12 6 Jan '08  
QuestionHow do you get the Default Gateway IP of a router? Pinmemberebonnettit11:31 21 Jun '07  
Generalusing SetIP only works for Gateway Pinmembermystique200012:03 24 Sep '05  
GeneralWoes on Win98 PinmemberMagnus Persson21:03 25 Apr '04  
GeneralRe: Woes on Win98 PinsussAnonymous22:38 21 Nov '04  
GeneralRe: Woes on Win98 Pinmemberjohn anvet21:41 25 Jul '08  
QuestionDNS servers? PinmemberSeveredlimb8:42 8 Feb '04  
AnswerHOw to change the IP of a linix machine? PinmemberThulasi ramu J0:26 15 Apr '04  
GeneralDifferent network adapters PinmemberAlois Kraus2:47 5 Jan '04  
GeneralRe: Different network adapters PinmemberWaldenL5:21 10 Oct '05  
GeneralRe: Different network adapters PinmemberRoland_W21:39 25 Jan '06  
Generalcorrect msdn link PinmemberDaniel Fisher (lennybacon)23:34 29 Dec '03  
GeneralRe: correct msdn link Pinmember Xmen 16:18 13 Mar '09  
QuestionHow do I to Control NetworkAdapter state (enable or disenable ) in c#? Pinmembermoonlight21cn200119:12 24 Dec '03  
AnswerRe: How do I to Control NetworkAdapter state (enable or disenable ) in c#? Pinmemberuwgmxbscdbuwgmxbscdb5:55 6 Jul '06  
GeneralRe: How do I to Control NetworkAdapter state (enable or disenable ) in c#? Pinmember--Simas--7:39 27 Apr '07  

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.

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