Csharp/CSharp Tutorial/Network/NetworkInterface

Материал из .Net Framework эксперт
Версия от 12:20, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Report information from NetworkInterface: IP address and DHCP Expires (DhcpLeaseLifetime)

using System;
using System.Net.NetworkInformation;
class MainClass
{
    static void Main()
    {
        if (!NetworkInterface.GetIsNetworkAvailable())
           return;
        NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
        foreach (NetworkInterface ni in interfaces)
        {
            Console.WriteLine("IP Addresses:");
            foreach (UnicastIPAddressInformation addr in ni.GetIPProperties().UnicastAddresses)
            {
                Console.WriteLine("- {0} (lease expires {1})", addr.Address, DateTime.Now + new TimeSpan(0, 0, (int)addr.DhcpLeaseLifetime));
            }
        }
    }
}
IP Addresses:
IP Addresses:
- 192.168.1.101 (lease expires 26/03/2007 8:28:25 AM)
IP Addresses:
- 127.0.0.1 (lease expires 31/12/1969 5:00:00 PM)

Report information from NetworkInterface: name, description, id, type, speed, status

using System;
using System.Net.NetworkInformation;
class MainClass
{
    static void Main()
    {
        if (!NetworkInterface.GetIsNetworkAvailable())
           return;
        NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
        foreach (NetworkInterface ni in interfaces)
        {
            
            Console.WriteLine("Interface Name: {0}", ni.Name);
            Console.WriteLine("    Description: {0}", ni.Description);
            Console.WriteLine("    ID: {0}", ni.Id);
            Console.WriteLine("    Type: {0}", ni.NetworkInterfaceType);
            Console.WriteLine("    Speed: {0}", ni.Speed);
            Console.WriteLine("    Status: {0}", ni.OperationalStatus);
        }
    }
}
Interface Name: Wireless Network Connection
    Description: Broadcom 802.11b/g WLAN - Packet Scheduler Miniport
    ID: {4FA13198-E7E5-4065-8395-EDC576EE1020}
    Type: Ethernet
    Speed: 54000000
    Status: Down
Interface Name: Local Area Connection
    Description: Realtek RTL8139 Family PCI Fast Ethernet NIC - Packet Scheduler Miniport
    ID: {0180EFAF-14BF-42AA-A274-6D8D766A0093}
    Type: Ethernet
    Speed: 100000000
    Status: Up
Interface Name: MS TCP Loopback interface
    Description: MS TCP Loopback interface
    ID: MS TCP Loopback interface
    Type: Loopback
    Speed: 10000000
    Status: Up

Report information from NetworkInterface: network statistics

using System;
using System.Net.NetworkInformation;
class MainClass
{
    static void Main()
    {
        if (!NetworkInterface.GetIsNetworkAvailable())
           return;
        NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
        foreach (NetworkInterface ni in interfaces)
        {
            
            Console.WriteLine("    Bytes Sent: {0}", ni.GetIPv4Statistics().BytesSent);
            Console.WriteLine("    Bytes Received: {0}", ni.GetIPv4Statistics().BytesReceived);
        }
    }
}
Bytes Sent: 0
    Bytes Received: 0
    Bytes Sent: 48385872
    Bytes Received: 1254629355
    Bytes Sent: 10523298
    Bytes Received: 10523298

Report information from NetworkInterface: physical address

using System;
using System.Net.NetworkInformation;
class MainClass
{
    static void Main()
    {
        if (!NetworkInterface.GetIsNetworkAvailable())
           return;
        NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
        foreach (NetworkInterface ni in interfaces)
        {
            Console.WriteLine("    Physical Address: {0}",  ni.GetPhysicalAddress().ToString());
        }
    }
}
Physical Address: 0014A51190DC
    Physical Address: 00C09FC5BD14
    Physical Address: