123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace LYFZ.WinAPI
- {
- /// <summary>
- /// 本地网卡对象
- /// </summary>
- public class LocalNetworkCardObjects
- {
- public LocalNetworkCardObjects()
- {
- System.Net.NetworkInformation.NetworkInterface[] fNetworkInterfaces = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
- foreach (System.Net.NetworkInformation.NetworkInterface adapter in fNetworkInterfaces)
- {
- NetworkCardInfo cardInfo = new NetworkCardInfo();
- #region " 网卡信息 "
- cardInfo.Id = adapter.Id;
- cardInfo.Name = adapter.Name;
- cardInfo.Description = adapter.Description;
- cardInfo.NetworkInterfaceType = adapter.NetworkInterfaceType;
- cardInfo.Speed = adapter.Speed;
- cardInfo.MACPhysicalAddress = adapter.GetPhysicalAddress();
- /*string fCardInfo = "";
- fCardInfo += ("-----------------------------------------------------------");
- fCardInfo += ("-- " + fCardType);
- fCardInfo += ("-----------------------------------------------------------");
- fCardInfo += ("Id .................. : " + adapter.Id); // 获取网络适配器的标识符
- fCardInfo += ("Name ................ : " + adapter.Name); // 获取网络适配器的名称
- fCardInfo += ("Description ......... : " + adapter.Description); // 获取接口的描述
- fCardInfo += ("Interface type ...... : " + adapter.NetworkInterfaceType); // 获取接口类型
- fCardInfo += ("Is receive only...... : " + adapter.IsReceiveOnly); // 获取 Boolean 值,该值指示网络接口是否设置为仅接收数据包。
- fCardInfo += ("Multicast............ : " + adapter.SupportsMulticast); // 获取 Boolean 值,该值指示是否启用网络接口以接收多路广播数据包。
- fCardInfo += ("Speed ............... : " + adapter.Speed); // 网络接口的速度
- fCardInfo += ("Physical Address .... : " + adapter.GetPhysicalAddress().ToString()); // MAC 地址 */
- System.Net.NetworkInformation.IPInterfaceProperties fIPInterfaceProperties = adapter.GetIPProperties();
- System.Net.NetworkInformation.UnicastIPAddressInformationCollection UnicastIPAddressInformationCollection = fIPInterfaceProperties.UnicastAddresses;
- foreach (System.Net.NetworkInformation.UnicastIPAddressInformation UnicastIPAddressInformation in UnicastIPAddressInformationCollection)
- {
- if (UnicastIPAddressInformation.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
- {
- cardInfo.IP4AddressList.Add(UnicastIPAddressInformation.Address);// Ip4 地址
- }
- else if (UnicastIPAddressInformation.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
- {
- cardInfo.IP6AddressList.Add(UnicastIPAddressInformation.Address);// Ip6 地址
- }
- else
- {
- cardInfo.OtherIPAddressList.Add(UnicastIPAddressInformation.Address);
- }
- }
- #endregion
- #region " 网卡类型 "
-
- string fRegistryKey = "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\" + adapter.Id + "\\Connection";
- Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(fRegistryKey, false);
- if (rk != null)
- {
- // 区分 PnpInstanceID
- // 如果前面有 PCI 就是本机的真实网卡
- // MediaSubType 为 01 则是常见网卡,02为无线网卡。
- string fPnpInstanceID = rk.GetValue("PnpInstanceID", "").ToString();
- int fMediaSubType = Convert.ToInt32(rk.GetValue("MediaSubType", 0));
- if (fPnpInstanceID.Length > 3 && fPnpInstanceID.Substring(0, 3) == "PCI")
- {
- cardInfo.CardType = NetworkCardType.物理网卡;
- this._PhysicalNICList.Add(cardInfo);
- }
- else if (fMediaSubType == 2)
- {
- cardInfo.CardType = NetworkCardType.无线网卡;
- this._WirelessLANList.Add(cardInfo);
- }
- else
- {
- cardInfo.CardType = NetworkCardType.虚拟网卡;
- this._VirtualNetworkCardList.Add(cardInfo);
- }
- }
- else {
- cardInfo.CardType = NetworkCardType.未知网卡;
- this._UnknownCardCList.Add(cardInfo);
- }
- #endregion
-
- }
- }
- List<NetworkCardInfo> _PhysicalNICList = new List<NetworkCardInfo>();
- /// <summary>
- /// 物理网卡集合
- /// </summary>
- public List<NetworkCardInfo> PhysicalNICList
- {
- get { return _PhysicalNICList; }
- set { _PhysicalNICList = value; }
- }
- List<NetworkCardInfo> _WirelessLANList = new List<NetworkCardInfo>();
- /// <summary>
- /// 无线网卡集合
- /// </summary>
- public List<NetworkCardInfo> WirelessLANList
- {
- get { return _WirelessLANList; }
- set { _WirelessLANList = value; }
- }
- List<NetworkCardInfo> _VirtualNetworkCardList = new List<NetworkCardInfo>();
- /// <summary>
- /// 虚拟网卡集合
- /// </summary>
- public List<NetworkCardInfo> VirtualNetworkCardList
- {
- get { return _VirtualNetworkCardList; }
- set { _VirtualNetworkCardList = value; }
- }
- List<NetworkCardInfo> _UnknownCardCList = new List<NetworkCardInfo>();
- /// <summary>
- /// 未知网卡集合
- /// </summary>
- public List<NetworkCardInfo> UnknownCardCList
- {
- get { return _UnknownCardCList; }
- set { _UnknownCardCList = value; }
- }
- }
- /// <summary>
- /// 网卡类型
- /// </summary>
- public enum NetworkCardType
- {
- 物理网卡=0,
- 无线网卡=1,
- 虚拟网卡=2,
- 未知网卡=3,
- }
- public class NetworkCardInfo
- {
- string id;
- /// <summary>
- /// 获取网络适配器的标识符
- /// </summary>
- public string Id
- {
- get { return id; }
- set { id = value; }
- }
- string name;
- /// <summary>
- /// 获取网络适配器的名称
- /// </summary>
- public string Name
- {
- get { return name; }
- set { name = value; }
- }
- string description;
- /// <summary>
- /// 获取接口的描述
- /// </summary>
- public string Description
- {
- get { return description; }
- set { description = value; }
- }
- System.Net.NetworkInformation.NetworkInterfaceType networkInterfaceType;
- /// <summary>
- /// 获取接口类型
- /// </summary>
- public System.Net.NetworkInformation.NetworkInterfaceType NetworkInterfaceType
- {
- get { return networkInterfaceType; }
- set { networkInterfaceType = value; }
- }
- long speed;
- /// <summary>
- /// 网络接口的速度
- /// </summary>
- public long Speed
- {
- get { return speed; }
- set { speed = value; }
- }
- private System.Net.NetworkInformation.PhysicalAddress mACPhysicalAddress;
- /// <summary>
- /// 返回此适配器的媒体访问控制 (MAC) 或物理地址。
- /// </summary>
- public System.Net.NetworkInformation.PhysicalAddress MACPhysicalAddress
- {
- get { return mACPhysicalAddress; }
- set { mACPhysicalAddress = value; }
- }
- List<System.Net.IPAddress> iP4AddressList=new List<System.Net.IPAddress>();
- /// <summary>
- /// IP4
- /// </summary>
- public List<System.Net.IPAddress> IP4AddressList
- {
- get { return iP4AddressList; }
- set { iP4AddressList = value; }
- }
- List<System.Net.IPAddress> iP6AddressList=new List<System.Net.IPAddress>();
- /// <summary>
- /// IP6
- /// </summary>
- public List<System.Net.IPAddress> IP6AddressList
- {
- get { return iP6AddressList; }
- set { iP6AddressList = value; }
- }
- List<System.Net.IPAddress> otherIPAddressList=new List<System.Net.IPAddress>();
- /// <summary>
- /// OtherIP 其它IP
- /// </summary>
- public List<System.Net.IPAddress> OtherIPAddressList
- {
- get { return otherIPAddressList; }
- set { otherIPAddressList = value; }
- }
- /// <summary>
- /// 获取所有IP
- /// </summary>
- /// <returns></returns>
- public List<System.Net.IPAddress> GetAllIPAddress()
- {
- List<System.Net.IPAddress> allIPAddress = new List<System.Net.IPAddress>();
- allIPAddress.AddRange(iP4AddressList);
- allIPAddress.AddRange(iP6AddressList);
- allIPAddress.AddRange(otherIPAddressList);
- return allIPAddress;
- }
- NetworkCardType cardType;
- /// <summary>
- /// 网卡类型
- /// </summary>
- public NetworkCardType CardType
- {
- get { return cardType; }
- set { cardType = value; }
- }
- }
- }
|