MAUI 第七天 Android和Windows 获取IP地址

目标:

1、在Android和Windows 获取IP地址。

代码:

@using System.Net
@using System.Net.Sockets
@using System.Net.NetworkInformation


    public void GetLocalIpAddress()
    {
        int index = 1;
        foreach (var ni in NetworkInterface.GetAllNetworkInterfaces())
        {
            if (ni.NetworkInterfaceType==NetworkInterfaceType.Wireless80211 || ni.NetworkInterfaceType==NetworkInterfaceType.Ethernet)
            {
                foreach (var ip in ni.GetIPProperties().UnicastAddresses)
                {
                    if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
                    {
                        index++;
                        iplist.Add(new IPModel { IP = ip.Address.ToString(), Index = index.ToString() });
                    }
                }
            }   
        }
    }

你可能感兴趣的:(MAUI,android,tcp/ip,网络)