NetcardInfoTool.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //************************************************************************
  2. // author: yuzhengyang
  3. // date: 2017.3.29 - 2017.7.12
  4. // desc: 网卡信息工具类
  5. // Copyright (c) yuzhengyang. All rights reserved.
  6. //************************************************************************
  7. using Azylee.Core.DataUtils.CollectionUtils;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Diagnostics;
  11. using System.Net;
  12. using System.Net.NetworkInformation;
  13. using System.Net.Sockets;
  14. using System.Text;
  15. namespace Azylee.Core.WindowsUtils.InfoUtils
  16. {
  17. public class NetCardInfoTool
  18. {
  19. /// <summary>
  20. /// 获取网卡信息
  21. /// 【名称、描述、物理地址(Mac)、Ip地址、网关地址】
  22. /// </summary>
  23. /// <returns></returns>
  24. public static List<Tuple<string, string, string, string, string>> GetNetworkCardInfo()
  25. {
  26. try
  27. {
  28. List<Tuple<string, string, string, string, string>> result = new List<Tuple<string, string, string, string, string>>();
  29. NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
  30. foreach (var item in adapters)
  31. {
  32. if (item.NetworkInterfaceType == NetworkInterfaceType.Ethernet || item.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
  33. {
  34. string _name = item.Name.Trim();
  35. string _desc = item.Description.Trim();
  36. string _mac = item.GetPhysicalAddress().ToString();
  37. //测试获取数据
  38. var x = item.GetIPProperties().UnicastAddresses;
  39. string _ip = item.GetIPProperties().UnicastAddresses.Count >= 1 ?
  40. item.GetIPProperties().UnicastAddresses[0].Address.ToString() : null;
  41. //更新IP为ipv4地址
  42. if (item.GetIPProperties().UnicastAddresses.Count > 0)
  43. _ip = item.GetIPProperties().UnicastAddresses[item.GetIPProperties().
  44. UnicastAddresses.Count - 1].Address.ToString();
  45. string _gateway = item.GetIPProperties().GatewayAddresses.Count >= 1 ?
  46. item.GetIPProperties().GatewayAddresses[0].Address.ToString() : null;
  47. result.Add(new Tuple<string, string, string, string, string>(_name, _desc, _mac, _ip, _gateway));
  48. }
  49. }
  50. return result;
  51. }
  52. catch (NetworkInformationException e)
  53. {
  54. return null;
  55. }
  56. }
  57. /// <summary>
  58. /// 获取网络连接操作状态
  59. /// </summary>
  60. /// <param name="mac"></param>
  61. /// <returns></returns>
  62. public static OperationalStatus GetOpStatus(string mac)
  63. {
  64. try
  65. {
  66. NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
  67. foreach (var item in adapters)
  68. {
  69. string _mac = item.GetPhysicalAddress().ToString();
  70. if(_mac.ToUpper() == mac.ToUpper())
  71. return item.OperationalStatus;
  72. }
  73. }
  74. catch { }
  75. return OperationalStatus.Unknown;
  76. }
  77. /// <summary>
  78. /// 获取网卡实例名称
  79. /// </summary>
  80. /// <returns></returns>
  81. public static string[] GetInstanceNames()
  82. {
  83. string[] instances = null;
  84. try
  85. {
  86. PerformanceCounterCategory performanceCounterCategory = new PerformanceCounterCategory("Network Interface");
  87. instances = performanceCounterCategory.GetInstanceNames();
  88. }
  89. catch { }
  90. return instances;
  91. }
  92. /// <summary>
  93. /// 获取本机IPv4的IP地址
  94. /// </summary>
  95. /// <returns></returns>
  96. public static List<IPAddress> GetIPv4Address()
  97. {
  98. List<IPAddress> hosts = new List<IPAddress>();
  99. try
  100. {
  101. var temp = Dns.GetHostAddresses(Dns.GetHostName());
  102. if (ListTool.HasElements(temp))
  103. {
  104. foreach (var t in temp)
  105. {
  106. if (t.AddressFamily == AddressFamily.InterNetwork)
  107. {
  108. hosts.Add(t);
  109. }
  110. }
  111. }
  112. }
  113. catch (Exception e) { }
  114. return hosts;
  115. }
  116. /// <summary>
  117. /// 获取本机IPv4的IP地址
  118. /// </summary>
  119. /// <returns></returns>
  120. public static List<string> GetAllIPv4Address()
  121. {
  122. List<string> hosts = new List<string>();
  123. try
  124. {
  125. var temp = Dns.GetHostAddresses(Dns.GetHostName());
  126. if (ListTool.HasElements(temp))
  127. {
  128. foreach (var t in temp)
  129. {
  130. if (t.AddressFamily == AddressFamily.InterNetwork)
  131. {
  132. hosts.Add(t.ToString());
  133. }
  134. }
  135. }
  136. }
  137. catch (Exception e) { }
  138. hosts.Add("0.0.0.0");
  139. hosts.Add("127.0.0.1");
  140. return hosts;
  141. }
  142. /// <summary>
  143. /// 全小写MAC地址
  144. /// </summary>
  145. /// <param name="s"></param>
  146. /// <returns></returns>
  147. public static string ShortMac(string s)
  148. {
  149. if (!string.IsNullOrWhiteSpace(s))
  150. {
  151. return s.Replace("-", "").Replace(":", "").ToLower();
  152. }
  153. return "";
  154. }
  155. /// <summary>
  156. /// 格式化MAC地址(大写、':' 分割)
  157. /// </summary>
  158. /// <param name="s"></param>
  159. /// <returns></returns>
  160. public static string MACFormat(string s, bool isUpper = true)
  161. {
  162. StringBuilder sb = new StringBuilder();
  163. if (!string.IsNullOrWhiteSpace(s))
  164. {
  165. if (s.Length == 12)
  166. {
  167. sb.Append(
  168. $"{s.Substring(0, 2)}:" +
  169. $"{s.Substring(2, 2)}:" +
  170. $"{s.Substring(4, 2)}:" +
  171. $"{s.Substring(6, 2)}:" +
  172. $"{s.Substring(8, 2)}:" +
  173. $"{s.Substring(10, 2)}");
  174. }
  175. }
  176. return isUpper ? sb.ToString().ToUpper() : sb.ToString().ToLower();
  177. }
  178. }
  179. }