using System;
using System.Collections.Generic;
using System.Collections;
using System.Globalization;

using System.Net;
using System.Net.NetworkInformation;

public class MyIpConfig
{
    private NetworkInterface[] interfaces = null;

    public static void Main(String[] args)
    {
        MyIpConfig myIpConfig = new MyIpConfig();
    }

    MyIpConfig()
    {
        // 取得本機電腦上所有網路介面卡的資訊
        interfaces = NetworkInterface.GetAllNetworkInterfaces();

        // 判斷本機電腦之網路介面卡是否有網路連線
        if (NetworkInterface.GetIsNetworkAvailable())
            Console.WriteLine("Network interface is up.");
        else
            Console.WriteLine("The network is not available.");

        ShowNetworkInterfaces();
    }

    private void ShowNetworkInterfaces()
    {
        // 取得本機電腦上所有網路介面卡的資訊
        interfaces = NetworkInterface.GetAllNetworkInterfaces();

        if (interfaces.Length == 0)
            Console.WriteLine("No network interface is found.");
        else
        {
            foreach (NetworkInterface adapter in interfaces)
            {
                // 取得網路介面卡的描述包括廠商、類型、商標及型號
                Console.WriteLine("====== "+adapter.Description+" =====");
                UpdateNetworkInformation(adapter);
                UpdateNetworkStatistics(adapter);
            }
        }
    }

    private void UpdateNetworkInformation(NetworkInterface pInterface)
    {
        // 取得支援IP通訊協定之網路介面卡的相關資訊
        IPInterfaceProperties ipProperties = pInterface.GetIPProperties();

        // 取得DHCP伺服器位址
        IPAddressCollection ipAddresses = ipProperties.DhcpServerAddresses;
        foreach (IPAddress ipAddress in ipAddresses)
            addIPAddress("DHCP伺服器", ipAddress);

        // 取得網路介面卡所設定之DNS伺服器位址
        ipAddresses = ipProperties.DnsAddresses;
        foreach (IPAddress ipAddress in ipAddresses)
            addIPAddress("DNS伺服器", ipAddress);

        // 取得WINS伺服器的位址
        ipAddresses = ipProperties.WinsServersAddresses;
        foreach (IPAddress ipAddress in ipAddresses)
            addIPAddress("WINS伺服器", ipAddress);

        // 取得網路介面卡的網路閘道位址
        GatewayIPAddressInformationCollection gatewayInfo = ipProperties.GatewayAddresses;
        foreach (GatewayIPAddressInformation info in gatewayInfo)
            addIPAddress("網路閘道", info.Address);

        // 取得網路介面卡的Anycast IP位址
        IPAddressInformationCollection anycastInfo = ipProperties.AnycastAddresses;
        foreach (IPAddressInformation info in anycastInfo)
            addIPAddress("Anycast", info.Address);

        // 取得網路介面卡的多點播送位址
        MulticastIPAddressInformationCollection multicastInfo = ipProperties.MulticastAddresses;
        foreach (MulticastIPAddressInformation info in multicastInfo)
            addIPAddress("多點播送", info.Address);

        // 取得網路介面卡的單點傳送位址
        UnicastIPAddressInformationCollection unicastInfo = ipProperties.UnicastAddresses;
        foreach (UnicastIPAddressInformation info in unicastInfo)
            addIPAddress("單點傳送", info.Address);
    }

    private void UpdateNetworkStatistics(NetworkInterface pInterface)
    {
        // 取得本機電腦支援IPv4通訊協定之網路介面卡的統計資料
        IPv4InterfaceStatistics ipv4Statistics = pInterface.GetIPv4Statistics();

        // 取得支援IP通訊協定之網路介面卡的相關資訊
        IPInterfaceProperties ipProperties = pInterface.GetIPProperties();

        NumberFormatInfo numberFormat = NumberFormatInfo.CurrentInfo;

        // DNS尾碼
        Console.WriteLine("DNS尾碼:" + ipProperties.DnsSuffix.ToString());

        // 取得網路介面卡所接收的位元組數目
        long received = ipv4Statistics.BytesReceived / 1024;
        Console.WriteLine("介面卡所接收的位元組數目:" + received.ToString("N0", numberFormat) + " KB");

        // 取得網路介面卡所傳送的位元組數目
        long sent = ipv4Statistics.BytesSent / 1024;
        Console.WriteLine("介面卡所傳送的位元組數目:" + sent.ToString("N0", numberFormat) + " KB");

        // 取得網路介面卡的速度
        Console.WriteLine("介面卡的速度:" + ConvertSpeedtoString(pInterface.Speed));

        // 取得網路介面卡目前的狀態，為OperationalStatus之列舉值
        Console.WriteLine("介面卡目前的狀態" + pInterface.OperationalStatus.ToString());

        // 判斷網路介面卡是否接收多點播送封包
        Console.WriteLine("是否接收多點播送封包" + pInterface.SupportsMulticast.ToString());
    }

    private void addIPAddress(string type, IPAddress ipAddress)
    {
        Console.WriteLine(type + ":" + ipAddress.ToString());
    }

    private string ConvertSpeedtoString(long speed)
    {
        switch (speed)
        {
            case 10000000:
                return "10 MB";
            case 11000000:
                return "11 MB";
            case 54000000:
                return "54 MB";
            case 100000000:
                return "100 MB";
            case 1000000000:
                return "1 GB";
            default:
                return speed.ToString(NumberFormatInfo.CurrentInfo);
        }
    }
}

