NetcardInfoTool.cs 7.5 KB

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