1.获取本机ipv4地址,使用控制台程序进行测试
IPHostEntry ipHost = Dns.GetHostByName(host);
string ipv4 = ipHost.AddressList[0].ToString();
Console.WriteLine(ipv4);
效果
2.获取所有IP地址
string host = Dns.GetHostName();
IPHostEntry ipHost = Dns.GetHostByName(host);
IPAddress[] ress = ipHost.AddressList;//返回与主机关联的ip地址列表
for (int i = 0; i < ress.Length; i++)//然后遍历数组
{
Console.WriteLine(ress[0]);
}