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

}

}

}

posted on 2019-03-05 23:50  艾普米song  阅读(233)  评论(0编辑  收藏  举报