获取网卡信息
引入的命名空间
代码如下:
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Text; 7 using System.Windows.Forms; 8 using System.Net; 9 using System.Net.NetworkInformation; 10 11 namespace WindowsFormsApplication5 12 { 13 public partial class Form1 : Form 14 { 15 public Form1() 16 { 17 InitializeComponent(); 18 } 19 20 private void Form1_Load(object sender, EventArgs e) 21 { 22 ShowAdapterInfo(); 23 } 24 25 /// <summary> 26 /// 显示网卡信息 27 /// </summary> 28 private void ShowAdapterInfo() 29 { 30 NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces(); 31 lst_NetworkAdapter.Items.Add("适配器个数:" + adapters.Length); 32 int index = 0; 33 34 foreach (NetworkInterface adapter in adapters) 35 { 36 index++; 37 //显示网络适配器描述信息、名称、类型、速度、MAC 地址 38 lst_NetworkAdapter.Items.Add("---------------------第" + index + "个适配器信息---------------------"); 39 lst_NetworkAdapter.Items.Add("描述信息:" + adapter.Name); 40 lst_NetworkAdapter.Items.Add("类型:" + adapter.NetworkInterfaceType); 41 lst_NetworkAdapter.Items.Add("速度:" + adapter.Speed / 1000 / 1000 + "MB"); 42 lst_NetworkAdapter.Items.Add("MAC 地址:" + adapter.GetPhysicalAddress()); 43 44 //获取IPInterfaceProperties实例 45 IPInterfaceProperties adapterProperties = adapter.GetIPProperties(); 46 47 //获取并显示DNS服务器IP地址信息 48 IPAddressCollection dnsServers = adapterProperties.DnsAddresses; 49 if (dnsServers.Count > 0) 50 { 51 foreach (IPAddress dns in dnsServers) 52 { 53 lst_NetworkAdapter.Items.Add("DNS 服务器IP地址:" + dns + "\n"); 54 } 55 } 56 else 57 { 58 lst_NetworkAdapter.Items.Add("DNS 服务器IP地址:" + "\n"); 59 } 60 } 61 } 62 } 63 }