| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- using System.Management;
- using System.Net;
- using System.Net.NetworkInformation;
- using System.Net.Sockets;
- namespace Muchinfo.PC.Common.Helpers
- {
- public class IPHelper
- {
- /// <summary>
- /// 获取IP
- /// </summary>
- /// <param name="ipSite">获取IP的站点</param>
- /// <param name="startString">开始字符串</param>
- /// <param name="endString">结束字符串</param>
- /// <returns>System.String.</returns>
- public static string GetIP(string ipSite = "", string startString = "", string endString = "")
- {
- //WebRequest request = null;
- try
- {
- //var uri = new Uri(ipSite);
- //request = WebRequest.Create(uri);
- //request.Timeout = 1000 * 5;
- //var response = request.GetResponse();
- //var stream = response.GetResponseStream();
- //if (stream != null)
- //{
- // var reader = new StreamReader(stream, Encoding.Default);
- // string result = reader.ReadToEnd();
- // int start = result.IndexOf(startString, StringComparison.Ordinal) + startString.Length;
- // int end = result.IndexOf(endString, start, System.StringComparison.Ordinal);
- // var ip = result.Substring(start, end - start);
- // response.Close();
- // return ip.Trim();
- //}
- //response.Close();
- return GetLocalIP();
- }
- catch
- {
- return GetLocalIP();
- }
- finally
- {
- //if (request != null) request.Abort();
- }
- }
- /// <summary>
- /// 获取MAC地址
- /// </summary>
- /// <returns>System.String.</returns>
- public static string GetMACAddress()
- {
- var address = GetMACAddressByService();
- if (string.IsNullOrWhiteSpace(address))
- {
- address = GetMACAddressByNetwork();
- }
- return address;
- }
- /// <summary>
- /// 获取MAC地址
- /// </summary>
- /// <returns>System.String.</returns>
- private static string GetMACAddressByService()
- {
- var str = string.Empty;
- try
- {
- ManagementClass mc;
- ManagementObjectCollection moc;
- mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
- moc = mc.GetInstances();
- foreach (ManagementObject mo in moc)
- {
- if ((bool)mo["IPEnabled"]) return mo["MacAddress"].ToString();
- }
- }
- catch
- {
- str = string.Empty;
- }
- return str;
- }
- /// <summary>
- /// 获取MAC地址
- /// </summary>
- /// <returns>System.String.</returns>
- public static string GetMACAddressByNetwork()
- {
- var nics = NetworkInterface.GetAllNetworkInterfaces();
- //var macAddress = new StringBuilder();
- foreach (NetworkInterface adapter in nics)
- {
- var mac = adapter.GetPhysicalAddress().ToString();
- if (!string.IsNullOrEmpty(mac) && adapter.OperationalStatus == OperationalStatus.Up)
- {
- return mac;
- //macAddress.Append(mac);
- //macAddress.Append(",");
- }
- }
- //return macAddress.ToString().TrimEnd(',');
- return string.Empty;
- }
- /// <summary>
- /// 打开Web网址
- /// </summary>
- /// <param name="url">网址</param>
- public static void OpenWebAddress(string url)
- {
- System.Diagnostics.Process.Start(url);
- }
- /// <summary>
- /// 获取本地IP
- /// </summary>
- /// <returns>System.String.</returns>
- private static string GetLocalIP()
- {
- string strHostName = Dns.GetHostName();
- var ipEntry = Dns.GetHostEntry(strHostName);
- foreach (IPAddress ipAddress in ipEntry.AddressList)
- {
- if (ipAddress.AddressFamily == AddressFamily.InterNetwork)
- {
- return ipAddress.ToString();
- }
- }
- return "-";
- }
- }
- }
|