码迷,mamicode.com
首页 > 其他好文 > 详细

获取本地ip

时间:2017-12-26 00:51:20      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:ipaddress   gpo   type   str   本地ip   internet   ipa   col   public   

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

标签:ipaddress   gpo   type   str   本地ip   internet   ipa   col   public   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!