首页 > 其他 > 详细

得到本地电脑IP4地址

时间:2019-03-06 00:51:29      阅读:234      评论:0      收藏:0      [点我收藏+]

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

}

}

}

得到本地电脑IP4地址

原文:https://www.cnblogs.com/pinecone-song/p/10480752.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!