Csharp/C Sharp by API/System.Net.NetworkInformation/NetworkInterface

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

NetworkInterface.GetIPProperties()

  
using System;
using System.Net.NetworkInformation;
class MainClass {
    static void Main() {
        if (NetworkInterface.GetIsNetworkAvailable()) {
            NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface ni in interfaces) {
                foreach (UnicastIPAddressInformation addr in ni.GetIPProperties().UnicastAddresses) {
                    Console.WriteLine("         - {0} (lease expires {1})", addr.Address, DateTime.Now + new TimeSpan(0, 0, (int)addr.DhcpLeaseLifetime));
                }
            }
        } else {
            Console.WriteLine("No network available.");
        }
    }
}


NetworkInterface.GetIPv4Statistics()

  
using System;
using System.Net.NetworkInformation;
class MainClass {
    static void Main() {
        if (NetworkInterface.GetIsNetworkAvailable()) {
            NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface ni in interfaces) {
                Console.WriteLine("    Bytes Sent: {0}", ni.GetIPv4Statistics().BytesSent);
            }
        } else {
            Console.WriteLine("No network available.");
        }
    }
}


NetworkInterface.GetIsNetworkAvailable

  
using System;
using System.Net.NetworkInformation;
class MainClass {
    static void Main() {
        if (NetworkInterface.GetIsNetworkAvailable()) {
            NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface ni in interfaces) {
                foreach (UnicastIPAddressInformation addr in ni.GetIPProperties().UnicastAddresses) {
                    Console.WriteLine("         - {0} (lease expires {1})", addr.Address, DateTime.Now + new TimeSpan(0, 0, (int)addr.DhcpLeaseLifetime));
                }
            }
        } else {
            Console.WriteLine("No network available.");
        }
    }
}


NetworkInterface.GetPhysicalAddress

  
using System;
using System.Net.NetworkInformation;
class MainClass {
    static void Main() {
        if (NetworkInterface.GetIsNetworkAvailable()) {
            NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface ni in interfaces) {
                Console.WriteLine("     Physical Address: {0}", ni.GetPhysicalAddress().ToString());
            }
        } else {
            Console.WriteLine("No network available.");
        }
    }
}


NetworkInterface.NetworkInterfaceType

  
using System;
using System.Net.NetworkInformation;
class MainClass {
    static void Main() {
        if (NetworkInterface.GetIsNetworkAvailable()) {
            NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface ni in interfaces) {
                Console.WriteLine("    Type: {0}", ni.NetworkInterfaceType);
            }
        } else {
            Console.WriteLine("No network available.");
        }
    }
}


NetworkInterface.OperationalStatus

  
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);
        }
    }
}


NetworkInterface.Speed

  
using System;
using System.Net.NetworkInformation;
class MainClass {
    static void Main() {
        if (NetworkInterface.GetIsNetworkAvailable()) {
            NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface ni in interfaces) {
                Console.WriteLine("     Speed: {0}", ni.Speed);
            }
        } else {
            Console.WriteLine("No network available.");
        }
    }
}