使用.NET MAUI从Android和iOS设备获取IP地址
我正在尝试使用.NET MAUI从Android设备获得IP地址。 (当前的Android Framework Net6.0 Android)
使用以下代码,我可以在该应用程序的第一次启动时获得IP。除非我重新启动手机,否则它不适用于启动应用程序。(在Samsung S10+ Android 12上进行了测试)
private void GetIPAddressClicked(object sender, EventArgs e)
{
string interfaceDescription = string.Empty;
var result = new List<IPAddress>();
try
{
var upAndNotLoopbackNetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces().Where(n => n.NetworkInterfaceType != NetworkInterfaceType.Loopback
&& n.OperationalStatus == OperationalStatus.Up);
foreach (var networkInterface in upAndNotLoopbackNetworkInterfaces)
{
var iPInterfaceProperties = networkInterface.GetIPProperties();
var unicastIpAddressInformation = iPInterfaceProperties.UnicastAddresses.FirstOrDefault(u => u.Address.AddressFamily == AddressFamily.InterNetwork);
if (unicastIpAddressInformation == null) continue;
result.Add(unicastIpAddressInformation.Address);
interfaceDescription += networkInterface.Description + "---";
}
}
catch (Exception ex)
{
Debug.WriteLine($"Unable to find IP: {ex.Message}");
}
finally
{
string allIpInfo = string.Empty;
foreach (var item in result)
{
allIpInfo += item.ToString() + "---";
}
IPAddress.Text = allIpInfo;
InterfaceName.Text = interfaceDescription;
}
在工作时,
FetfaceScription =“ -------”
AllipInfo =“ 192.168.50.50.112 ------ 127.0。 0.1 ---“
当它不起作用时,
交织时 - &gt; rmnet_data0
allipinfo ---&gt; 192.0.0.2
我不确定这里有什么问题。我该如何解决?还是有更好的方法来获取IP地址? 谢谢。
I am trying to get IP address from android device using .NET MAUI. (Current android framework net6.0-android)
With the below code, I can get the IP with the first launch of the application. And it does not work with following launch of the application unless I reboot my phone.(Tested on Samsung S10+ Android 12)
private void GetIPAddressClicked(object sender, EventArgs e)
{
string interfaceDescription = string.Empty;
var result = new List<IPAddress>();
try
{
var upAndNotLoopbackNetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces().Where(n => n.NetworkInterfaceType != NetworkInterfaceType.Loopback
&& n.OperationalStatus == OperationalStatus.Up);
foreach (var networkInterface in upAndNotLoopbackNetworkInterfaces)
{
var iPInterfaceProperties = networkInterface.GetIPProperties();
var unicastIpAddressInformation = iPInterfaceProperties.UnicastAddresses.FirstOrDefault(u => u.Address.AddressFamily == AddressFamily.InterNetwork);
if (unicastIpAddressInformation == null) continue;
result.Add(unicastIpAddressInformation.Address);
interfaceDescription += networkInterface.Description + "---";
}
}
catch (Exception ex)
{
Debug.WriteLine(quot;Unable to find IP: {ex.Message}");
}
finally
{
string allIpInfo = string.Empty;
foreach (var item in result)
{
allIpInfo += item.ToString() + "---";
}
IPAddress.Text = allIpInfo;
InterfaceName.Text = interfaceDescription;
}
When it works,
interfaceDescription = "------"
allIpInfo = "192.168.50.112---127.0.0.1---"
When it does not work,
interfaceDescription ---> rmnet_data0
allIpInfo ---> 192.0.0.2
I am not sure what is wrong here. How can I fix this? Or is there a better way to get the IP address?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在Alternativa中,我可以推荐此方法。它仅返回IP地址,而不像上面代码中的整个列表一样返回。
导入这些库
,并将此代码
用于Windows您可以使用
In alternativa, I can recommend this method. It returns only the IP address and not the entire list like in the code above.
Import these libraries
And use this code
For Windows u can use