首页 > 其他 > 详细

获取本地ip

时间:2017-12-25 23:56:39      阅读:308      评论:0      收藏:0      [点我收藏+]

public void GetLocalIP(string username)
{
List<string> strIPs = new List<string>();
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in nics)
{

if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
{
var mac = adapter.GetPhysicalAddress(); Console.WriteLine(mac);
IPInterfaceProperties ip = adapter.GetIPProperties();
UnicastIPAddressInformationCollection ipCollection = ip.UnicastAddresses;
foreach (UnicastIPAddressInformation ipadd in ipCollection)
{
//InterNetwork IPV4地址
//InterNetworkV6 IPV6地址
if (ipadd.Address.AddressFamily == AddressFamily.InterNetwork)
{ //判断是否为ipv4
Console.WriteLine(ipadd.Address.ToString());
strIPs.Add(ipadd.Address.ToString());
}
}
}
}
string IP = string.Join(";", strIPs);
}

获取本地ip

原文:https://www.cnblogs.com/ZxtIsCnblogs/p/7755915.html

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