title | author | date | CreateTime | categories |
---|---|---|---|---|
dotnet 获取本机 IP 地址方法 | lindexi | 2019-09-09 15:56:33 +0800 | 2019-09-05 14:34:12 +0800 | dotnet |
本文告诉大家如何在 C# .NET 获取本机 IP 地址
有两个获取方法,第一个方法是通过 DNS 获取
var host = Dns.GetHostEntry(Dns.GetHostName()); foreach (var ip in host.AddressList) { // 下面的判断过滤 IP v4 地址 if (ip.AddressFamily == AddressFamily.InterNetwork) { Console.WriteLine(ip.ToString()); } }
第二个方法可以过滤指定是 WIFI 的地址还是有限网的地址
foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces()) { if (( item.NetworkInterfaceType == NetworkInterfaceType.Ethernet // 有线网络 || item.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 // 无线 wifi 网络 ) && item.OperationalStatus == OperationalStatus.Up) { foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses) { if (ip.Address.AddressFamily == AddressFamily.InterNetwork) { Console.WriteLine(ip.Address.ToString()); } } } }
过滤方法通过 NetworkInterfaceType 判断