using System.Linq;
using System.Net;
using System.Net.Sockets;
namespace winform_udp
{
public class common
{
/// <summary>
/// 得到本地电脑IP4地址
/// </summary>
/// <returns></returns>
public static string GetIP4()
{
// 获取本地计算机主机名
string hostName = Dns.GetHostName();
// 获得主机地址信息
IPHostEntry host = Dns.GetHostEntry(hostName);
IPAddress IPstr = host.AddressList
// 从主机的IP地址列表中,找到IP4地址
.Where(x => x.AddressFamily == AddressFamily.InterNetwork)
// 转换为List,并获取List中第一个
.ToList().FirstOrDefault();
return IPstr.ToString();
}
}
}
原文地址:https://www.cnblogs.com/pinecone-song/p/10480752.html
时间: 2024-10-12 04:12:44