安然不哭-西坝阿超个人博客

做技术一定要一颗恒心,这样才不会半途而废。

C# 获取本机网卡信息、个数、描述信息、类型、速度等

2018-4-30 钟建超

引入命名空间

using System.Net.NetworkInformation;  
using System.Net;  



完整代码:


namespace NetworkInterfaceExample  
{  
    public partial class Form1 : Form  
    {  
        public Form1()  
        {  
            InitializeComponent();  
        }  
  
        private void Form1_Load(object sender, EventArgs e)  
        {  
            ShowAdapterInfo();  
        }  
  
        /// <summary>  
        /// 显示网卡信息  
        /// </summary>  
        private void ShowAdapterInfo()  
        {  
            NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();  
            lst_NetworkAdapter.Items.Add("适配器个数:" + adapters.Length);  
            int index = 0;  
  
            foreach (NetworkInterface adapter in adapters)  
            {  
                index++;  
                //显示网络适配器描述信息、名称、类型、速度、MAC 地址  
                lst_NetworkAdapter.Items.Add("---------------------第" + index + "个适配器信息---------------------");  
                lst_NetworkAdapter.Items.Add("描述信息:" + adapter.Name);  
                lst_NetworkAdapter.Items.Add("类型:" + adapter.NetworkInterfaceType);  
                lst_NetworkAdapter.Items.Add("速度:" + adapter.Speed / 1000 / 1000 + "MB");  
                lst_NetworkAdapter.Items.Add("MAC 地址:" + adapter.GetPhysicalAddress());  
                  
                //获取IPInterfaceProperties实例  
                IPInterfaceProperties adapterProperties = adapter.GetIPProperties();  
                  
                //获取并显示DNS服务器IP地址信息  
                IPAddressCollection dnsServers = adapterProperties.DnsAddresses;  
                if (dnsServers.Count > 0)  
                {  
                    foreach (IPAddress dns in dnsServers)  
                    {  
                        lst_NetworkAdapter.Items.Add("DNS 服务器IP地址:" + dns + "\n");  
                    }  
                }  
                else  
                {  
                    lst_NetworkAdapter.Items.Add("DNS 服务器IP地址:" + "\n");  
                }  
            }  
        }  
    }  
}  



标签: c#