得到本地电脑IP4地址

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();

}

}

}

猜你喜欢

转载自www.cnblogs.com/pinecone-song/p/10480752.html