很完整的来了!!
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();
}
///
/// 显示网卡信息
///
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");
}
}
}
}
}