我需要找到运行我的软件的手机的IP地址。我本以为这是直截了当的但是在论坛上搜索似乎(令人难以置信的是)在Windows Phone 7中没有这种方法 - 但是,这在Windows Phone 8中有所改变吗?任何帮助将不胜感激。
答案 0 :(得分:16)
是的,现在可以在WP8中使用,而无需使用WP7所需的多播解决方案。请注意,您的手机上将有多个网络接口(例如,我的WP8仿真器上有三个)
public static IPAddress Find()
{
List<string> ipAddresses = new List<string>();
var hostnames = NetworkInformation.GetHostNames();
foreach (var hn in hostnames)
{
if (hn.IPInformation != null)
{
string ipAddress = hn.DisplayName;
ipAddresses.Add(ipAddress);
}
}
IPAddress address = IPAddress.Parse(ipAddresses[0]);
return address;
}
HTH
答案 1 :(得分:3)
当然有办法找到手机的IP地址。这是一篇MSDN博客博客文章,解释了如何执行此操作:Finding Your Own IP Address On Windows Phone Mango
我刚刚在我的诺基亚Lumia 920(Windows Phone 8)上测试过,它运行得很好。但是,由于使用了多播IP,这仅适用于WiFi。
答案 2 :(得分:0)
Windows RT代码
public static string GetIpAddress()
{
var address = "";
var icp = NetworkInformation.GetInternetConnectionProfile();
if (icp != null && icp.NetworkAdapter != null)
{
var hostname =
NetworkInformation.GetHostNames()
.SingleOrDefault(
hn =>
hn.IPInformation != null && hn.IPInformation.NetworkAdapter != null
&& hn.IPInformation.NetworkAdapter.NetworkAdapterId
== icp.NetworkAdapter.NetworkAdapterId);
if (hostname != null)
{
address = hostname.CanonicalName;
}
}
return address;
}