原来获取本机IP地址的方法(方法1):
var items = Dns.GetHostEntry(Dns.GetHostName()).AddressList .Where(s => s.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork && !System.Net.IPAddress.IsLoopback(s)) .Select(s => s.ToString()) .ToArray();
同事反应,在麒麟系统下,调用Dns.GetHostEntry方法时会报错。
在网上搜到了另外的方法(方法2):
var addressList = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces() .Select(p => p.GetIPProperties()) .SelectMany(p => p.UnicastAddresses) .Where(p => p.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork && !System.Net.IPAddress.IsLoopback(p.Address)) .Select(s => s.Address.ToString()).ToArray();
采用了.NET 6编写,在Ubuntu 22.04.1系统下试了一下,方法2能执行成功,正确获取IP地址。
但是试了一下,方法1也能正常获取IP地址。
标签:Address,麒麟,System,银河,Dns,IP地址,Net,AddressFamily From: https://www.cnblogs.com/wzwyc/p/16668423.html