获取本地ip

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

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