Get current ip-address Xamarin.Forms ( Cross Platform)

心不动则不痛 提交于 2019-12-22 08:14:00

问题


How get current ip-address for Xamarin.Forms ( Cross Platform) ? WifiManager I can't use and can't WifiManager include?


回答1:


public string MyIp;

foreach (IPAddress adress in Dns.GetHostAddresses(Dns.GetHostName()))
{
    MyIp = "IP Adress: " + adress.ToString();
    SettingsTab.IP_Adress = MyIp;
    break;
}



回答2:


try to do this:

    public static string GetIPAddress()
    {
        var AllNetworkInterfaces = Collections.List(Java.Net.NetworkInterface.NetworkInterfaces);
        var IPAddres = "";
        foreach (var interfaces in AllNetworkInterfaces)
        {
            if (!(interfaces as Java.Net.NetworkInterface).Name.Contains("eth0")) continue;

            var AddressInterface = (interfaces as Java.Net.NetworkInterface).InterfaceAddresses;
            foreach (var AInterface in AddressInterface)
            {
                if(AInterface.Broadcast != null)
                    IPAddres = AInterface.Address.HostAddress;
            }
        }
            return IPAddres;
    }



回答3:


Or try this:

var IpAddress = Dns.GetHostAddresses(Dns.GetHostName()).FirstOrDefault();

if (IpAddress != null)
{
    return IpAddress.ToString();
}


来源:https://stackoverflow.com/questions/27904375/get-current-ip-address-xamarin-forms-cross-platform

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!