NetcardInfoTool.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Net.NetworkInformation;
  7. using System.Net.Sockets;
  8. using System.Text;
  9. using Y.Utils.DataUtils.Collections;
  10. namespace Y.Utils.NetUtils.NetInfoUtils
  11. {
  12. public class NetCardInfoTool
  13. {
  14. /// <summary>
  15. /// 获取网卡信息
  16. /// 名称,描述,物理地址(Mac),Ip地址,网关地址
  17. /// </summary>
  18. /// <returns></returns>
  19. public static List<Tuple<string, string, string, string, string>> GetNetworkCardInfo()
  20. {
  21. try
  22. {
  23. List<Tuple<string, string, string, string, string>> result = new List<Tuple<string, string, string, string, string>>();
  24. NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
  25. foreach (var item in adapters)
  26. {
  27. if (item.NetworkInterfaceType == NetworkInterfaceType.Ethernet || item.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
  28. {
  29. string _name = item.Name;
  30. string _desc = item.Description;
  31. string _mac = item.GetPhysicalAddress().ToString();
  32. string _ip = item.GetIPProperties().UnicastAddresses.Count >= 2 ?
  33. item.GetIPProperties().UnicastAddresses[1].Address.ToString() : null;
  34. string _gateway = item.GetIPProperties().GatewayAddresses.Count >= 1 ?
  35. item.GetIPProperties().GatewayAddresses[0].Address.ToString() : null;
  36. result.Add(new Tuple<string, string, string, string, string>(_name, _desc, _mac, _ip, _gateway));
  37. }
  38. }
  39. return result;
  40. }
  41. catch (NetworkInformationException e)
  42. {
  43. return null;
  44. }
  45. }
  46. /// <summary>
  47. /// 获取网卡实例名称
  48. /// </summary>
  49. /// <returns></returns>
  50. public static string[] GetInstanceNames()
  51. {
  52. string[] instances = null;
  53. try
  54. {
  55. PerformanceCounterCategory performanceCounterCategory = new PerformanceCounterCategory("Network Interface");
  56. instances = performanceCounterCategory.GetInstanceNames();
  57. }
  58. catch { }
  59. return instances;
  60. }
  61. /// <summary>
  62. /// 获取本机IPv4的IP地址
  63. /// </summary>
  64. /// <returns></returns>
  65. public static List<IPAddress> GetIPv4Address()
  66. {
  67. List<IPAddress> hosts = new List<IPAddress>();
  68. try
  69. {
  70. var temp = Dns.GetHostAddresses(Dns.GetHostName());
  71. if (ListTool.HasElements(temp))
  72. {
  73. foreach (var t in temp)
  74. {
  75. if (t.AddressFamily == AddressFamily.InterNetwork)
  76. {
  77. hosts.Add(t);
  78. }
  79. }
  80. }
  81. }
  82. catch (Exception e) { }
  83. return hosts;
  84. }
  85. /// <summary>
  86. /// 获取本机IPv4的IP地址
  87. /// </summary>
  88. /// <returns></returns>
  89. public static List<string> GetAllIPv4Address()
  90. {
  91. List<string> hosts = new List<string>();
  92. try
  93. {
  94. var temp = Dns.GetHostAddresses(Dns.GetHostName());
  95. if (ListTool.HasElements(temp))
  96. {
  97. foreach (var t in temp)
  98. {
  99. if (t.AddressFamily == AddressFamily.InterNetwork)
  100. {
  101. hosts.Add(t.ToString());
  102. }
  103. }
  104. }
  105. }
  106. catch (Exception e) { }
  107. hosts.Add("0.0.0.0");
  108. hosts.Add("127.0.0.1");
  109. return hosts;
  110. }
  111. }
  112. }