NetcardInfoTool.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 Azylee.Core.DataUtils.StringUtils;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Diagnostics;
  12. using System.Linq;
  13. using System.Net;
  14. using System.Net.NetworkInformation;
  15. using System.Net.Sockets;
  16. using System.Runtime.InteropServices;
  17. using System.Text;
  18. namespace Azylee.Core.WindowsUtils.InfoUtils
  19. {
  20. public class NetCardInfoTool
  21. {
  22. public static List<NetworkInterface> NetworkInterfaces = new List<NetworkInterface>();
  23. public static List<NetworkInterface> GetNetworkInterfaces()
  24. {
  25. try
  26. {
  27. NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
  28. if (Ls.Ok(adapters))
  29. {
  30. foreach (var item in adapters)
  31. {
  32. try
  33. {
  34. if (NetworkInterfaces.Any(x => x.Id == item.Id)) { }
  35. else { NetworkInterfaces.Add(item); }
  36. }
  37. catch { }
  38. }
  39. }
  40. }
  41. catch { }
  42. return NetworkInterfaces;
  43. }
  44. /// <summary>
  45. /// 获取网卡信息
  46. /// 【名称、描述、物理地址(Mac)、Ip地址、网关地址】
  47. /// </summary>
  48. /// <returns></returns>
  49. public static List<Tuple<string, string, string, string, string>> GetNetworkCardInfo()
  50. {
  51. try
  52. {
  53. List<Tuple<string, string, string, string, string>> result = new List<Tuple<string, string, string, string, string>>();
  54. List<NetworkInterface> adapters = GetNetworkInterfaces();
  55. foreach (var item in adapters)
  56. {
  57. if (item.NetworkInterfaceType == NetworkInterfaceType.Ethernet || item.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
  58. {
  59. string _name = item.Name.Trim();
  60. string _desc = item.Description.Trim();
  61. string _mac = item.GetPhysicalAddress().ToString();
  62. //测试获取数据
  63. var x = item.GetIPProperties().UnicastAddresses;
  64. string _ip = item.GetIPProperties().UnicastAddresses.Count >= 1 ?
  65. item.GetIPProperties().UnicastAddresses[0].Address.ToString() : null;
  66. //更新IP为ipv4地址
  67. if (item.GetIPProperties().UnicastAddresses.Count > 0)
  68. _ip = item.GetIPProperties().UnicastAddresses[item.GetIPProperties().
  69. UnicastAddresses.Count - 1].Address.ToString();
  70. string _gateway = item.GetIPProperties().GatewayAddresses.Count >= 1 ?
  71. item.GetIPProperties().GatewayAddresses[0].Address.ToString() : null;
  72. result.Add(new Tuple<string, string, string, string, string>(_name, _desc, _mac, _ip, _gateway));
  73. }
  74. }
  75. return result;
  76. }
  77. catch (NetworkInformationException e)
  78. {
  79. return null;
  80. }
  81. }
  82. /// <summary>
  83. /// 获取网卡信息
  84. /// 【id,名称、描述、物理地址(Mac)、Ip地址、网关地址】
  85. /// </summary>
  86. /// <returns></returns>
  87. public static List<Tuple<string, string, string, string, string, Guid>> GetNetworkCardInfoId()
  88. {
  89. try
  90. {
  91. List<Tuple<string, string, string, string, string, Guid>> result = new List<Tuple<string, string, string, string, string, Guid>>();
  92. List<NetworkInterface> adapters = GetNetworkInterfaces();
  93. foreach (var item in adapters)
  94. {
  95. if (item.NetworkInterfaceType == NetworkInterfaceType.Ethernet || item.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
  96. {
  97. string _name = item.Name.Trim();
  98. string _desc = item.Description.Trim();
  99. string _mac = item.GetPhysicalAddress().ToString();
  100. Guid _id = Guid.NewGuid();//随机一个ID(Empty可能导致重复性错误)
  101. Guid.TryParse(item.Id, out _id);
  102. //测试获取数据
  103. var x = item.GetIPProperties().UnicastAddresses;
  104. string _ip = item.GetIPProperties().UnicastAddresses.Count >= 1 ?
  105. item.GetIPProperties().UnicastAddresses[0].Address.ToString() : null;
  106. //更新IP为ipv4地址
  107. if (item.GetIPProperties().UnicastAddresses.Count > 0)
  108. _ip = item.GetIPProperties().UnicastAddresses[item.GetIPProperties().
  109. UnicastAddresses.Count - 1].Address.ToString();
  110. string _gateway = item.GetIPProperties().GatewayAddresses.Count >= 1 ?
  111. item.GetIPProperties().GatewayAddresses[0].Address.ToString() : null;
  112. result.Add(new Tuple<string, string, string, string, string, Guid>(_name, _desc, _mac, _ip, _gateway, _id));
  113. }
  114. }
  115. return result;
  116. }
  117. catch (NetworkInformationException e)
  118. {
  119. return null;
  120. }
  121. }
  122. /// <summary>
  123. /// 获取网络连接状态
  124. /// </summary>
  125. /// <param name="dwFlag"></param>
  126. /// <param name="dwReserved"></param>
  127. /// <returns></returns>
  128. [DllImport("winInet.dll")]
  129. private static extern bool InternetGetConnectedState(ref int dwFlag, int dwReserved);
  130. /// <summary>
  131. /// 获取网络连接状态
  132. /// </summary>
  133. /// <returns></returns>
  134. public static bool LocalConnectionStatus()
  135. {
  136. try
  137. {
  138. int INTERNET_CONNECTION_MODEM = 1;
  139. int INTERNET_CONNECTION_LAN = 2;
  140. int dwFlag = 0;
  141. if (InternetGetConnectedState(ref dwFlag, 0)) return true;
  142. }
  143. catch { }
  144. return false;
  145. }
  146. /// <summary>
  147. /// 获取网络连接操作状态
  148. /// </summary>
  149. /// <param name="mac"></param>
  150. /// <returns></returns>
  151. public static OperationalStatus GetOpStatus(string mac)
  152. {
  153. try
  154. {
  155. NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
  156. foreach (var item in adapters)
  157. {
  158. string _mac = item.GetPhysicalAddress().ToString();
  159. if (_mac.ToUpper() == mac.ToUpper())
  160. return item.OperationalStatus;
  161. }
  162. }
  163. catch { }
  164. return OperationalStatus.Unknown;
  165. }
  166. /// <summary>
  167. /// 获取网卡实例名称
  168. /// </summary>
  169. /// <returns></returns>
  170. public static string[] GetInstanceNames()
  171. {
  172. string[] instances = null;
  173. try
  174. {
  175. PerformanceCounterCategory performanceCounterCategory = new PerformanceCounterCategory("Network Interface");
  176. instances = performanceCounterCategory.GetInstanceNames();
  177. }
  178. catch { }
  179. return instances;
  180. }
  181. /// <summary>
  182. /// 获取本机IPv4的IP地址
  183. /// </summary>
  184. /// <returns></returns>
  185. public static List<IPAddress> GetIPv4Address()
  186. {
  187. List<IPAddress> hosts = new List<IPAddress>();
  188. try
  189. {
  190. var temp = Dns.GetHostAddresses(Dns.GetHostName());
  191. if (ListTool.HasElements(temp))
  192. {
  193. foreach (var t in temp)
  194. {
  195. if (t.AddressFamily == AddressFamily.InterNetwork)
  196. {
  197. hosts.Add(t);
  198. }
  199. }
  200. }
  201. }
  202. catch (Exception e) { }
  203. return hosts;
  204. }
  205. /// <summary>
  206. /// 获取本机IPv4的IP地址
  207. /// </summary>
  208. /// <returns></returns>
  209. public static List<string> GetAllIPv4Address()
  210. {
  211. List<string> hosts = new List<string>();
  212. try
  213. {
  214. var temp = Dns.GetHostAddresses(Dns.GetHostName());
  215. if (ListTool.HasElements(temp))
  216. {
  217. foreach (var t in temp)
  218. {
  219. if (t.AddressFamily == AddressFamily.InterNetwork)
  220. {
  221. hosts.Add(t.ToString());
  222. }
  223. }
  224. }
  225. }
  226. catch (Exception e) { }
  227. hosts.Add("0.0.0.0");
  228. hosts.Add("127.0.0.1");
  229. return hosts;
  230. }
  231. /// <summary>
  232. /// 全小写MAC地址
  233. /// </summary>
  234. /// <param name="s"></param>
  235. /// <returns></returns>
  236. public static string ShortMac(string s)
  237. {
  238. try
  239. {
  240. if (Str.Ok(s)) return s.Replace(" ", "").Replace("-", "").Replace(":", "").ToLower();
  241. }
  242. catch { }
  243. return s;
  244. }
  245. /// <summary>
  246. /// 格式化MAC地址(大写、':' 分割)
  247. /// </summary>
  248. /// <param name="s"></param>
  249. /// <returns></returns>
  250. public static string MACFormat(string s, bool isUpper = true)
  251. {
  252. StringBuilder sb = new StringBuilder();
  253. if (!string.IsNullOrWhiteSpace(s))
  254. {
  255. if (s.Length == 12)
  256. {
  257. sb.Append(
  258. $"{s.Substring(0, 2)}:" +
  259. $"{s.Substring(2, 2)}:" +
  260. $"{s.Substring(4, 2)}:" +
  261. $"{s.Substring(6, 2)}:" +
  262. $"{s.Substring(8, 2)}:" +
  263. $"{s.Substring(10, 2)}");
  264. }
  265. }
  266. return isUpper ? sb.ToString().ToUpper() : sb.ToString().ToLower();
  267. }
  268. }
  269. }