C#获得网卡信息 NetworkInterface IPInterfaceProperties

System.Net.NetworkInformation下的

1:NetworkInterface类,提供网络适配器的配置和统计信息。

可以通过它检测本机配置了多少网卡,哪些网络连接可用,获得网卡的MAC地址和速度等。

此类封装本地计算机上的网络接口(也称作适配器)的数据。不需创建此类的实例;GetAllNetworkInterfaces 方法返回一个数组,对于本地计算机上的每个网络接口,该数组中都包含一个此类的实例。

2:IPInterfaceProperties类 提供有关支持 Internet 协议版本 4 (IPv4) 或 Internet 协议版本 6 (IPv6) 的网络接口的信息。

此类可用于访问支持 IPv4 或 IPv6 的网络接口的配置和地址信息。不要创建此类的实例,这些实例将由 GetIPProperties 方法返回。

若要访问 IPv4 特定属性,请使用 GetIPv4Properties 方法返回的对象。若要访问 IPv6 特定属性,请使用 GetIPv6Properties 方法返回的对象

 1     void Start () {
 2         //网卡信息类
 3         NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
 4         foreach (NetworkInterface adap in adapters)
 5         {
 6             Debug.Log("CardName::" + adap.Name + " /Speed::" + adap.Speed + " /MAC::" + BitConverter.ToString(adap.GetPhysicalAddress().GetAddressBytes()));
 7             IPInterfaceProperties ipProperties = adap.GetIPProperties();
 8             GatewayIPAddressInformationCollection gateways = ipProperties.GatewayAddresses;
 9             foreach (var tmp in gateways)
10             {
11                 Debug.Log("Gateway>>>"+tmp.Address);
12             }
13             IPAddressCollection dnsAddress = ipProperties.DnsAddresses;
14             foreach (IPAddress tmp in dnsAddress)
15             {
16                 Debug.Log("DNS>>>" + BitConverter.ToString(tmp.GetAddressBytes()));
17             }
18         }
19     }

//output

NetworkInterface>>>  https://msdn.microsoft.com/zh-cn/library/system.net.networkinformation.networkinterface%28v=vs.110%29.aspx

IPInterfaceProperties>>>  https://msdn.microsoft.com/zh-cn/library/system.net.networkinformation.ipinterfaceproperties%28v=vs.110%29.aspx

原文地址:https://www.cnblogs.com/2Yous/p/5797700.html