新思想

C# 网络信息 源代码

C# 网络信息 源代码

NetworkInformation

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.NetworkInformation;
using System.Globalization;

namespace NetworkInformation
{
    public partial class Form1 : Form
    {
        private NetworkInterface[] networkInterfaces = null;
        private NetworkInterface currentInterface = null;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(NetworkChange_NetworkAddressChanged);
            NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAvailabilityChanged);

            networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();

            UpdateNetworkAvailability(NetworkInterface.GetIsNetworkAvailable());

            UpdateNetworkInformation();
        }

        private void updateInfoTimer_Tick(object sender, EventArgs e)
        {
            UpdateNicStats();
        }

        private void UpdateNicStats()
        {
            // Get the IPv4    statistics for the currently selected interface.
            IPv4InterfaceStatistics ipStats = currentInterface.GetIPv4Statistics();

            NumberFormatInfo numberFormat = NumberFormatInfo.CurrentInfo;

            long bytesReceivedInKB = ipStats.BytesReceived / 1024;
            long bytesSentInKB = ipStats.BytesSent / 1024;

            this.speedTextLabel.Text = GetSpeedString(currentInterface.Speed);
            this.bytesReceivedTextLabel.Text = bytesReceivedInKB.ToString("N0", numberFormat) + " KB";
            this.bytesSentTextLabel.Text = bytesSentInKB.ToString("N0", numberFormat) + " KB";

            this.operationalStatusTextLabel.Text = currentInterface.OperationalStatus.ToString();
            this.supportsMulticastTextLabel.Text = currentInterface.SupportsMulticast.ToString();
        }

        static private string GetSpeedString(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);
            }
        }

        private void UpdateCurrentNicInformation()
        {
            IPInterfaceProperties ipProperties = currentInterface.GetIPProperties();
            this.dnsSuffixTextLabel.Text = ipProperties.DnsSuffix.ToString();
            this.networkInterfaceTypeLabel.Text = currentInterface.NetworkInterfaceType.ToString();
            this.physicalAddressLabel.Text = currentInterface.GetPhysicalAddress().ToString();

            this.addressListView.Items.Clear();
            IPAddressInformationCollection anycastInfo = ipProperties.AnycastAddresses;
            foreach (IPAddressInformation info in anycastInfo)
                InsertAddress(info.Address, "Anycast");
            UnicastIPAddressInformationCollection unicastInfo = ipProperties.UnicastAddresses;
            foreach (UnicastIPAddressInformation info in unicastInfo)
                InsertAddress(info.Address, "Unicast");
            MulticastIPAddressInformationCollection multicastInfo = ipProperties.MulticastAddresses;
            foreach (MulticastIPAddressInformation info in multicastInfo)
                InsertAddress(info.Address, "Multicast");
            GatewayIPAddressInformationCollection gatewayInfo = ipProperties.GatewayAddresses;
            foreach (GatewayIPAddressInformation info in gatewayInfo)
                InsertAddress(info.Address, "Gateway");

            IPAddressCollection ipAddresses = ipProperties.WinsServersAddresses;
            InsertAddresses(ipAddresses, "WINS Server");
            ipAddresses = ipProperties.DhcpServerAddresses;
            InsertAddresses(ipAddresses, "DHCP Server");
            ipAddresses = ipProperties.DnsAddresses;
            InsertAddresses(ipAddresses, "DNS Server");
        }

        private void InsertAddresses(IPAddressCollection ipAddresses, string addressType)
        {
            foreach (IPAddress ipAddress in ipAddresses)
                InsertAddress(ipAddress, addressType);
        }

        private void InsertAddress(IPAddress ipAddress, string addressType)
        {
            string[] listViewInformation = new string[2];
            listViewInformation[0] = ipAddress.ToString();
            listViewInformation[1] = addressType;

            ListViewItem item = new ListViewItem(listViewInformation);
            addressListView.Items.Add(item);
        }

        private void UpdateNetworkInformation()
        {
            networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
            this.networkInterfacesComboBox.Items.Clear();
            foreach (NetworkInterface networkInterface in networkInterfaces)
                networkInterfacesComboBox.Items.Add(networkInterface.Description);

            if (networkInterfaces.Length == 0)
            {
                this.networkInterfacesComboBox.Items.Add("设备里没有找到任何 NIC ");
            }
            else
            {
                currentInterface = networkInterfaces[0];
                UpdateCurrentNicInformation();
            }

            this.networkInterfacesComboBox.SelectedIndex = 0;

        }

        private void UpdateNetworkAvailability(bool isNetworkAvailable)
        {
            if (isNetworkAvailable)
                this.networkAvailabilityTextLabel.Text = "至少有一个网卡启用.";
            else
                this.networkAvailabilityTextLabel.Text = "当前没有可用网络.";
        }

        private delegate void NetworkAddressChangedCallback();
        private delegate void NetworkAvailabilityCallback(bool isNetworkAvailable);

        private void NetworkChange_NetworkAddressChanged(object sender, EventArgs e)
        {
            this.Invoke(new NetworkAddressChangedCallback(UpdateNetworkInformation));
        }

        private void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
        {
            this.Invoke(new NetworkAvailabilityCallback(UpdateNetworkAvailability), new object[1] { e.IsAvailable });
        }

        private void OnSelectionChanged(object sender, System.EventArgs e)
        {
            currentInterface = networkInterfaces[networkInterfacesComboBox.SelectedIndex];
            UpdateCurrentNicInformation();
        }

    }
}

源程序下载

执行程序下载

posted on 2015-12-01 00:03  新思想  阅读(675)  评论(0)    收藏  举报

导航