Csharp/CSharp Tutorial/Network/Win32 NetworkAdapterConfiguration — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 15:31, 26 мая 2010

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);
            }
        }
    }
}
Gateway: 192.168.1.1
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an ob
ject.
   at MainClass.Main()

Get IP address 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[] addresses = (string[])mo["IPAddress"];
            foreach(string ipaddress in addresses)
            {
                Console.WriteLine("IP Address: {0}", ipaddress);
            }
        }
    }
}
IP Address: 192.168.1.101
IP Address: 0.0.0.0

Get Network card description and Mac address 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 )
        {
            Console.WriteLine("Network Card: {0}", mo["Description"]);
            Console.WriteLine("MAC Address: {0}", mo["MACAddress"]);
        }
    }
}
Network Card: Realtek RTL8139 Family PCI Fast Ethernet NIC - Packet Scheduler Miniport
MAC Address: 00:C0:9F:C5:BD:14
Network Card: Broadcom 802.11b/g WLAN - Packet Scheduler Miniport
MAC Address: 00:14:A5:11:90:DC

Get subnet mask 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[] subnets = (string[])mo["IPSubnet"];
            foreach(string subnet in subnets)
            {
                Console.WriteLine("Subnet Mask: {0}", subnet);
            }
        }
    }
}
Subnet Mask: 255.255.255.0
Subnet Mask: