.net获取本地ip地址

时间:2023-03-08 15:43:16
.net获取本地ip地址

整理代码,.net获取本地ip地址,代码如下:

 string name = Dns.GetHostName();
IPHostEntry IpEntry = Dns.GetHostEntry(name);
string ip = "";
for (int i = ; i < IpEntry.AddressList.Length; i++)
{
//从IP地址列表中筛选出IPv4类型的IP地址
//AddressFamily.InterNetwork表示此IP为IPv4,
//AddressFamily.InterNetworkV6表示此地址为IPv6类型
if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
{
ip= IpEntry.AddressList[i].ToString();
}
}