Get default gateway from Win32_NetworkAdapterConfiguration : Win32_NetworkAdapterConfiguration : Network C# Examples


C# Examples » Network » Win32_NetworkAdapterConfiguration »

 

Get default gateway from Win32_NetworkAdapterConfiguration









    
using  System;
using  System.Management;

class  MainClass
{
        public  static  void  Main()
        {
                ManagementObjectSearcher  query  =  new  ManagementObjectSearcher("SELECT  *  FROM  Win32_NetworkAdapterConfiguration  WHERE  IPEnabled  =  'TRUE'")  ;
                ManagementObjectCollection  queryCollection  =  query.Get();
                foreach(  ManagementObject  mo  in  queryCollection  )
                {

                        string[]  defaultgateways  =  (string[])mo["DefaultIPGateway"];

                        foreach(string  defaultgateway  in  defaultgateways)
                        {
                                Console.WriteLine("Gateway:  {0}",  defaultgateway);
                        }
                }
        }
}
    
   
  
   



Output

Gateway: 192.168.1.1

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an ob
ject.
   at MainClass.Main()


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Network
» Win32_NetworkAdapterConfiguration