/// <summary> /// 获取本机IP地址 /// </summary> /// <returns>本机IP地址</returns> public static string GetLocalIP() { try { string HostName = Dns.GetHostName(); //获取到主机名 IPHostEntry IpEntry = Dns.GetHostEntry(HostName); for (int i = 0; i < IpEntry.AddressList.Length; i++) { //从IP地址列表中筛选出IPv4类型的IP地址 //AddressFamily.InterNetwork表示此IP为IPv4, //AddressFamily.InterNetworkV6表示此地址为IPv6类型 if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork) { Console.WriteLine(IpEntry.AddressList[i].ToString()); return IpEntry.AddressList[i].ToString(); } } return ""; } catch (Exception ex) { Console.WriteLine(ex.ToString()); return ex.Message; } }
获取本机的IP地址
内容版权声明:除非注明,否则皆为本站原创文章。