NetcardInfoTool.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. /// <returns></returns>
  61. public static string[] GetInstanceNames()
  62. {
  63. string[] instances = null;
  64. try
  65. {
  66. PerformanceCounterCategory performanceCounterCategory = new PerformanceCounterCategory("Network Interface");
  67. instances = performanceCounterCategory.GetInstanceNames();
  68. }
  69. catch { }
  70. return instances;
  71. }
  72. /// <summary>
  73. /// 获取本机IPv4的IP地址
  74. /// </summary>
  75. /// <returns></returns>
  76. public static List<IPAddress> GetIPv4Address()
  77. {
  78. List<IPAddress> hosts = new List<IPAddress>();
  79. try
  80. {
  81. var temp = Dns.GetHostAddresses(Dns.GetHostName());
  82. if (ListTool.HasElements(temp))
  83. {
  84. foreach (var t in temp)
  85. {
  86. if (t.AddressFamily == AddressFamily.InterNetwork)
  87. {
  88. hosts.Add(t);
  89. }
  90. }
  91. }
  92. }
  93. catch (Exception e) { }
  94. return hosts;
  95. }
  96. /// <summary>
  97. /// 获取本机IPv4的IP地址
  98. /// </summary>
  99. /// <returns></returns>
  100. public static List<string> GetAllIPv4Address()
  101. {
  102. List<string> hosts = new List<string>();
  103. try
  104. {
  105. var temp = Dns.GetHostAddresses(Dns.GetHostName());
  106. if (ListTool.HasElements(temp))
  107. {
  108. foreach (var t in temp)
  109. {
  110. if (t.AddressFamily == AddressFamily.InterNetwork)
  111. {
  112. hosts.Add(t.ToString());
  113. }
  114. }
  115. }
  116. }
  117. catch (Exception e) { }
  118. hosts.Add("0.0.0.0");
  119. hosts.Add("127.0.0.1");
  120. return hosts;
  121. }
  122. /// <summary>
  123. /// 全小写MAC地址
  124. /// </summary>
  125. /// <param name="s"></param>
  126. /// <returns></returns>
  127. public static string ShortMac(string s)
  128. {
  129. if (!string.IsNullOrWhiteSpace(s))
  130. {
  131. return s.Replace("-", "").Replace(":", "").ToLower();
  132. }
  133. return "";
  134. }
  135. /// <summary>
  136. /// 格式化MAC地址(大写、':' 分割)
  137. /// </summary>
  138. /// <param name="s"></param>
  139. /// <returns></returns>
  140. public static string MACFormat(string s, bool isUpper = true)
  141. {
  142. StringBuilder sb = new StringBuilder();
  143. if (!string.IsNullOrWhiteSpace(s))
  144. {
  145. if (s.Length == 12)
  146. {
  147. sb.Append(
  148. $"{s.Substring(0, 2)}:" +
  149. $"{s.Substring(2, 2)}:" +
  150. $"{s.Substring(4, 2)}:" +
  151. $"{s.Substring(6, 2)}:" +
  152. $"{s.Substring(8, 2)}:" +
  153. $"{s.Substring(10, 2)}");
  154. }
  155. }
  156. return isUpper ? sb.ToString().ToUpper() : sb.ToString().ToLower();
  157. }
  158. }
  159. }