IPHelper.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System.Management;
  2. using System.Net;
  3. using System.Net.NetworkInformation;
  4. using System.Net.Sockets;
  5. namespace Muchinfo.PC.Common.Helpers
  6. {
  7. public class IPHelper
  8. {
  9. /// <summary>
  10. /// 获取IP
  11. /// </summary>
  12. /// <param name="ipSite">获取IP的站点</param>
  13. /// <param name="startString">开始字符串</param>
  14. /// <param name="endString">结束字符串</param>
  15. /// <returns>System.String.</returns>
  16. public static string GetIP(string ipSite = "", string startString = "", string endString = "")
  17. {
  18. //WebRequest request = null;
  19. try
  20. {
  21. //var uri = new Uri(ipSite);
  22. //request = WebRequest.Create(uri);
  23. //request.Timeout = 1000 * 5;
  24. //var response = request.GetResponse();
  25. //var stream = response.GetResponseStream();
  26. //if (stream != null)
  27. //{
  28. // var reader = new StreamReader(stream, Encoding.Default);
  29. // string result = reader.ReadToEnd();
  30. // int start = result.IndexOf(startString, StringComparison.Ordinal) + startString.Length;
  31. // int end = result.IndexOf(endString, start, System.StringComparison.Ordinal);
  32. // var ip = result.Substring(start, end - start);
  33. // response.Close();
  34. // return ip.Trim();
  35. //}
  36. //response.Close();
  37. return GetLocalIP();
  38. }
  39. catch
  40. {
  41. return GetLocalIP();
  42. }
  43. finally
  44. {
  45. //if (request != null) request.Abort();
  46. }
  47. }
  48. /// <summary>
  49. /// 获取MAC地址
  50. /// </summary>
  51. /// <returns>System.String.</returns>
  52. public static string GetMACAddress()
  53. {
  54. var address = GetMACAddressByService();
  55. if (string.IsNullOrWhiteSpace(address))
  56. {
  57. address = GetMACAddressByNetwork();
  58. }
  59. return address;
  60. }
  61. /// <summary>
  62. /// 获取MAC地址
  63. /// </summary>
  64. /// <returns>System.String.</returns>
  65. private static string GetMACAddressByService()
  66. {
  67. var str = string.Empty;
  68. try
  69. {
  70. ManagementClass mc;
  71. ManagementObjectCollection moc;
  72. mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
  73. moc = mc.GetInstances();
  74. foreach (ManagementObject mo in moc)
  75. {
  76. if ((bool)mo["IPEnabled"]) return mo["MacAddress"].ToString();
  77. }
  78. }
  79. catch
  80. {
  81. str = string.Empty;
  82. }
  83. return str;
  84. }
  85. /// <summary>
  86. /// 获取MAC地址
  87. /// </summary>
  88. /// <returns>System.String.</returns>
  89. public static string GetMACAddressByNetwork()
  90. {
  91. var nics = NetworkInterface.GetAllNetworkInterfaces();
  92. //var macAddress = new StringBuilder();
  93. foreach (NetworkInterface adapter in nics)
  94. {
  95. var mac = adapter.GetPhysicalAddress().ToString();
  96. if (!string.IsNullOrEmpty(mac) && adapter.OperationalStatus == OperationalStatus.Up)
  97. {
  98. return mac;
  99. //macAddress.Append(mac);
  100. //macAddress.Append(",");
  101. }
  102. }
  103. //return macAddress.ToString().TrimEnd(',');
  104. return string.Empty;
  105. }
  106. /// <summary>
  107. /// 打开Web网址
  108. /// </summary>
  109. /// <param name="url">网址</param>
  110. public static void OpenWebAddress(string url)
  111. {
  112. System.Diagnostics.Process.Start(url);
  113. }
  114. /// <summary>
  115. /// 获取本地IP
  116. /// </summary>
  117. /// <returns>System.String.</returns>
  118. private static string GetLocalIP()
  119. {
  120. string strHostName = Dns.GetHostName();
  121. var ipEntry = Dns.GetHostEntry(strHostName);
  122. foreach (IPAddress ipAddress in ipEntry.AddressList)
  123. {
  124. if (ipAddress.AddressFamily == AddressFamily.InterNetwork)
  125. {
  126. return ipAddress.ToString();
  127. }
  128. }
  129. return "-";
  130. }
  131. }
  132. }