获取当前 ip 地址 Xamarin.Forms(跨平台)
Get current ip-address Xamarin.Forms ( Cross Platform)
如何获取 Xamarin.Forms 的当前 IP 地址(跨平台)? WifiManager 我不能使用并且不能包含 WifiManager?
public string MyIp;
foreach (IPAddress adress in Dns.GetHostAddresses(Dns.GetHostName()))
{
MyIp = "IP Adress: " + adress.ToString();
SettingsTab.IP_Adress = MyIp;
break;
}
尝试这样做:
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;
}
或者试试这个:
var IpAddress = Dns.GetHostAddresses(Dns.GetHostName()).FirstOrDefault();
if (IpAddress != null)
{
return IpAddress.ToString();
}
如何获取 Xamarin.Forms 的当前 IP 地址(跨平台)? WifiManager 我不能使用并且不能包含 WifiManager?
public string MyIp;
foreach (IPAddress adress in Dns.GetHostAddresses(Dns.GetHostName()))
{
MyIp = "IP Adress: " + adress.ToString();
SettingsTab.IP_Adress = MyIp;
break;
}
尝试这样做:
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;
}
或者试试这个:
var IpAddress = Dns.GetHostAddresses(Dns.GetHostName()).FirstOrDefault();
if (IpAddress != null)
{
return IpAddress.ToString();
}