LocalNetworkCardObjects.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace LYFZ.WinAPI
  6. {
  7. /// <summary>
  8. /// 本地网卡对象
  9. /// </summary>
  10. public class LocalNetworkCardObjects
  11. {
  12. public LocalNetworkCardObjects()
  13. {
  14. System.Net.NetworkInformation.NetworkInterface[] fNetworkInterfaces = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
  15. foreach (System.Net.NetworkInformation.NetworkInterface adapter in fNetworkInterfaces)
  16. {
  17. NetworkCardInfo cardInfo = new NetworkCardInfo();
  18. #region " 网卡信息 "
  19. cardInfo.Id = adapter.Id;
  20. cardInfo.Name = adapter.Name;
  21. cardInfo.Description = adapter.Description;
  22. cardInfo.NetworkInterfaceType = adapter.NetworkInterfaceType;
  23. cardInfo.Speed = adapter.Speed;
  24. cardInfo.MACPhysicalAddress = adapter.GetPhysicalAddress();
  25. /*string fCardInfo = "";
  26. fCardInfo += ("-----------------------------------------------------------");
  27. fCardInfo += ("-- " + fCardType);
  28. fCardInfo += ("-----------------------------------------------------------");
  29. fCardInfo += ("Id .................. : " + adapter.Id); // 获取网络适配器的标识符
  30. fCardInfo += ("Name ................ : " + adapter.Name); // 获取网络适配器的名称
  31. fCardInfo += ("Description ......... : " + adapter.Description); // 获取接口的描述
  32. fCardInfo += ("Interface type ...... : " + adapter.NetworkInterfaceType); // 获取接口类型
  33. fCardInfo += ("Is receive only...... : " + adapter.IsReceiveOnly); // 获取 Boolean 值,该值指示网络接口是否设置为仅接收数据包。
  34. fCardInfo += ("Multicast............ : " + adapter.SupportsMulticast); // 获取 Boolean 值,该值指示是否启用网络接口以接收多路广播数据包。
  35. fCardInfo += ("Speed ............... : " + adapter.Speed); // 网络接口的速度
  36. fCardInfo += ("Physical Address .... : " + adapter.GetPhysicalAddress().ToString()); // MAC 地址 */
  37. System.Net.NetworkInformation.IPInterfaceProperties fIPInterfaceProperties = adapter.GetIPProperties();
  38. System.Net.NetworkInformation.UnicastIPAddressInformationCollection UnicastIPAddressInformationCollection = fIPInterfaceProperties.UnicastAddresses;
  39. foreach (System.Net.NetworkInformation.UnicastIPAddressInformation UnicastIPAddressInformation in UnicastIPAddressInformationCollection)
  40. {
  41. if (UnicastIPAddressInformation.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
  42. {
  43. cardInfo.IP4AddressList.Add(UnicastIPAddressInformation.Address);// Ip4 地址
  44. }
  45. else if (UnicastIPAddressInformation.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
  46. {
  47. cardInfo.IP6AddressList.Add(UnicastIPAddressInformation.Address);// Ip6 地址
  48. }
  49. else
  50. {
  51. cardInfo.OtherIPAddressList.Add(UnicastIPAddressInformation.Address);
  52. }
  53. }
  54. #endregion
  55. #region " 网卡类型 "
  56. string fRegistryKey = "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\" + adapter.Id + "\\Connection";
  57. Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(fRegistryKey, false);
  58. if (rk != null)
  59. {
  60. // 区分 PnpInstanceID
  61. // 如果前面有 PCI 就是本机的真实网卡
  62. // MediaSubType 为 01 则是常见网卡,02为无线网卡。
  63. string fPnpInstanceID = rk.GetValue("PnpInstanceID", "").ToString();
  64. int fMediaSubType = Convert.ToInt32(rk.GetValue("MediaSubType", 0));
  65. if (fPnpInstanceID.Length > 3 && fPnpInstanceID.Substring(0, 3) == "PCI")
  66. {
  67. cardInfo.CardType = NetworkCardType.物理网卡;
  68. this._PhysicalNICList.Add(cardInfo);
  69. }
  70. else if (fMediaSubType == 2)
  71. {
  72. cardInfo.CardType = NetworkCardType.无线网卡;
  73. this._WirelessLANList.Add(cardInfo);
  74. }
  75. else
  76. {
  77. cardInfo.CardType = NetworkCardType.虚拟网卡;
  78. this._VirtualNetworkCardList.Add(cardInfo);
  79. }
  80. }
  81. else {
  82. cardInfo.CardType = NetworkCardType.未知网卡;
  83. this._UnknownCardCList.Add(cardInfo);
  84. }
  85. #endregion
  86. }
  87. }
  88. List<NetworkCardInfo> _PhysicalNICList = new List<NetworkCardInfo>();
  89. /// <summary>
  90. /// 物理网卡集合
  91. /// </summary>
  92. public List<NetworkCardInfo> PhysicalNICList
  93. {
  94. get { return _PhysicalNICList; }
  95. set { _PhysicalNICList = value; }
  96. }
  97. List<NetworkCardInfo> _WirelessLANList = new List<NetworkCardInfo>();
  98. /// <summary>
  99. /// 无线网卡集合
  100. /// </summary>
  101. public List<NetworkCardInfo> WirelessLANList
  102. {
  103. get { return _WirelessLANList; }
  104. set { _WirelessLANList = value; }
  105. }
  106. List<NetworkCardInfo> _VirtualNetworkCardList = new List<NetworkCardInfo>();
  107. /// <summary>
  108. /// 虚拟网卡集合
  109. /// </summary>
  110. public List<NetworkCardInfo> VirtualNetworkCardList
  111. {
  112. get { return _VirtualNetworkCardList; }
  113. set { _VirtualNetworkCardList = value; }
  114. }
  115. List<NetworkCardInfo> _UnknownCardCList = new List<NetworkCardInfo>();
  116. /// <summary>
  117. /// 未知网卡集合
  118. /// </summary>
  119. public List<NetworkCardInfo> UnknownCardCList
  120. {
  121. get { return _UnknownCardCList; }
  122. set { _UnknownCardCList = value; }
  123. }
  124. }
  125. /// <summary>
  126. /// 网卡类型
  127. /// </summary>
  128. public enum NetworkCardType
  129. {
  130. 物理网卡=0,
  131. 无线网卡=1,
  132. 虚拟网卡=2,
  133. 未知网卡=3,
  134. }
  135. public class NetworkCardInfo
  136. {
  137. string id;
  138. /// <summary>
  139. /// 获取网络适配器的标识符
  140. /// </summary>
  141. public string Id
  142. {
  143. get { return id; }
  144. set { id = value; }
  145. }
  146. string name;
  147. /// <summary>
  148. /// 获取网络适配器的名称
  149. /// </summary>
  150. public string Name
  151. {
  152. get { return name; }
  153. set { name = value; }
  154. }
  155. string description;
  156. /// <summary>
  157. /// 获取接口的描述
  158. /// </summary>
  159. public string Description
  160. {
  161. get { return description; }
  162. set { description = value; }
  163. }
  164. System.Net.NetworkInformation.NetworkInterfaceType networkInterfaceType;
  165. /// <summary>
  166. /// 获取接口类型
  167. /// </summary>
  168. public System.Net.NetworkInformation.NetworkInterfaceType NetworkInterfaceType
  169. {
  170. get { return networkInterfaceType; }
  171. set { networkInterfaceType = value; }
  172. }
  173. long speed;
  174. /// <summary>
  175. /// 网络接口的速度
  176. /// </summary>
  177. public long Speed
  178. {
  179. get { return speed; }
  180. set { speed = value; }
  181. }
  182. private System.Net.NetworkInformation.PhysicalAddress mACPhysicalAddress;
  183. /// <summary>
  184. /// 返回此适配器的媒体访问控制 (MAC) 或物理地址。
  185. /// </summary>
  186. public System.Net.NetworkInformation.PhysicalAddress MACPhysicalAddress
  187. {
  188. get { return mACPhysicalAddress; }
  189. set { mACPhysicalAddress = value; }
  190. }
  191. List<System.Net.IPAddress> iP4AddressList=new List<System.Net.IPAddress>();
  192. /// <summary>
  193. /// IP4
  194. /// </summary>
  195. public List<System.Net.IPAddress> IP4AddressList
  196. {
  197. get { return iP4AddressList; }
  198. set { iP4AddressList = value; }
  199. }
  200. List<System.Net.IPAddress> iP6AddressList=new List<System.Net.IPAddress>();
  201. /// <summary>
  202. /// IP6
  203. /// </summary>
  204. public List<System.Net.IPAddress> IP6AddressList
  205. {
  206. get { return iP6AddressList; }
  207. set { iP6AddressList = value; }
  208. }
  209. List<System.Net.IPAddress> otherIPAddressList=new List<System.Net.IPAddress>();
  210. /// <summary>
  211. /// OtherIP 其它IP
  212. /// </summary>
  213. public List<System.Net.IPAddress> OtherIPAddressList
  214. {
  215. get { return otherIPAddressList; }
  216. set { otherIPAddressList = value; }
  217. }
  218. /// <summary>
  219. /// 获取所有IP
  220. /// </summary>
  221. /// <returns></returns>
  222. public List<System.Net.IPAddress> GetAllIPAddress()
  223. {
  224. List<System.Net.IPAddress> allIPAddress = new List<System.Net.IPAddress>();
  225. allIPAddress.AddRange(iP4AddressList);
  226. allIPAddress.AddRange(iP6AddressList);
  227. allIPAddress.AddRange(otherIPAddressList);
  228. return allIPAddress;
  229. }
  230. NetworkCardType cardType;
  231. /// <summary>
  232. /// 网卡类型
  233. /// </summary>
  234. public NetworkCardType CardType
  235. {
  236. get { return cardType; }
  237. set { cardType = value; }
  238. }
  239. }
  240. }