C# Mono Android获取本机Ip地址
/// <summary>
/// 获取本机IP地址。
/// </summary>
/// <returns></returns>
static public String GetLocalIpAddress()
{
try
{
Java.Util.IEnumeration intfs = Java.Net.NetworkInterface.NetworkInterfaces;
while (intfs.HasMoreElements)
{
Java.Net.NetworkInterface intf = (Java.Net.NetworkInterface)intfs.NextElement();
Java.Util.IEnumeration ipAddrs = intf.InetAddresses;
while (ipAddrs.HasMoreElements && !intf.IsLoopback)
{
Java.Net.InetAddress inetAddress = (Java.Net.InetAddress)ipAddrs.NextElement();
if (inetAddress is Java.Net.Inet4Address && !inetAddress.IsLoopbackAddress && !inetAddress.IsLinkLocalAddress)
{
return inetAddress.HostAddress;
}
}
};
}
catch (Java.Net.SocketException) { }
catch (Exception) { }
return null;
}