NetcardInfoTool.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. int INTERNET_CONNECTION_MODEM = 1;
  73. int INTERNET_CONNECTION_LAN = 2;
  74. int dwFlag = 0;
  75. if (InternetGetConnectedState(ref dwFlag, 0))
  76. {
  77. return true;
  78. }
  79. else
  80. {
  81. return false;
  82. }
  83. }
  84. /// <summary>
  85. /// 获取网络连接操作状态
  86. /// </summary>
  87. /// <param name="mac"></param>
  88. /// <returns></returns>
  89. public static OperationalStatus GetOpStatus(string mac)
  90. {
  91. try
  92. {
  93. NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
  94. foreach (var item in adapters)
  95. {
  96. string _mac = item.GetPhysicalAddress().ToString();
  97. if(_mac.ToUpper() == mac.ToUpper())
  98. return item.OperationalStatus;
  99. }
  100. }
  101. catch { }
  102. return OperationalStatus.Unknown;
  103. }
  104. /// <summary>
  105. /// 获取网卡实例名称
  106. /// </summary>
  107. /// <returns></returns>
  108. public static string[] GetInstanceNames()
  109. {
  110. string[] instances = null;
  111. try
  112. {
  113. PerformanceCounterCategory performanceCounterCategory = new PerformanceCounterCategory("Network Interface");
  114. instances = performanceCounterCategory.GetInstanceNames();
  115. }
  116. catch { }
  117. return instances;
  118. }
  119. /// <summary>
  120. /// 获取本机IPv4的IP地址
  121. /// </summary>
  122. /// <returns></returns>
  123. public static List<IPAddress> GetIPv4Address()
  124. {
  125. List<IPAddress> hosts = new List<IPAddress>();
  126. try
  127. {
  128. var temp = Dns.GetHostAddresses(Dns.GetHostName());
  129. if (ListTool.HasElements(temp))
  130. {
  131. foreach (var t in temp)
  132. {
  133. if (t.AddressFamily == AddressFamily.InterNetwork)
  134. {
  135. hosts.Add(t);
  136. }
  137. }
  138. }
  139. }
  140. catch (Exception e) { }
  141. return hosts;
  142. }
  143. /// <summary>
  144. /// 获取本机IPv4的IP地址
  145. /// </summary>
  146. /// <returns></returns>
  147. public static List<string> GetAllIPv4Address()
  148. {
  149. List<string> hosts = new List<string>();
  150. try
  151. {
  152. var temp = Dns.GetHostAddresses(Dns.GetHostName());
  153. if (ListTool.HasElements(temp))
  154. {
  155. foreach (var t in temp)
  156. {
  157. if (t.AddressFamily == AddressFamily.InterNetwork)
  158. {
  159. hosts.Add(t.ToString());
  160. }
  161. }
  162. }
  163. }
  164. catch (Exception e) { }
  165. hosts.Add("0.0.0.0");
  166. hosts.Add("127.0.0.1");
  167. return hosts;
  168. }
  169. /// <summary>
  170. /// 全小写MAC地址
  171. /// </summary>
  172. /// <param name="s"></param>
  173. /// <returns></returns>
  174. public static string ShortMac(string s)
  175. {
  176. if (!string.IsNullOrWhiteSpace(s))
  177. {
  178. return s.Replace("-", "").Replace(":", "").ToLower();
  179. }
  180. return "";
  181. }
  182. /// <summary>
  183. /// 格式化MAC地址(大写、':' 分割)
  184. /// </summary>
  185. /// <param name="s"></param>
  186. /// <returns></returns>
  187. public static string MACFormat(string s, bool isUpper = true)
  188. {
  189. StringBuilder sb = new StringBuilder();
  190. if (!string.IsNullOrWhiteSpace(s))
  191. {
  192. if (s.Length == 12)
  193. {
  194. sb.Append(
  195. $"{s.Substring(0, 2)}:" +
  196. $"{s.Substring(2, 2)}:" +
  197. $"{s.Substring(4, 2)}:" +
  198. $"{s.Substring(6, 2)}:" +
  199. $"{s.Substring(8, 2)}:" +
  200. $"{s.Substring(10, 2)}");
  201. }
  202. }
  203. return isUpper ? sb.ToString().ToUpper() : sb.ToString().ToLower();
  204. }
  205. }
  206. }