NetcardInfoTool.cs 4.4 KB

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