using System; using System.Collections.Generic; using System.Linq; using System.Net.Sockets; using System.Net; using System.Text; using System.Threading.Tasks; using System.Net.NetworkInformation; using System.Runtime.InteropServices; using System.Configuration; namespace XdCxRhDW.Core { public static class IpHelper { /// /// 获取本地IP /// /// public static string GetLocalIp() { try { string localIP = ConfigurationManager.AppSettings["LocalIP"].Trim();//优先使用本地配置的IP if(string.IsNullOrWhiteSpace(localIP)) { using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0)) { socket.Connect("8.8.8.8", 65530); IPEndPoint endPoint = socket.LocalEndPoint as IPEndPoint; localIP = endPoint.Address.ToString(); } } return localIP; } catch { return "127.0.0.1"; } } } }