我需要找到我的软件正在运行的手机的IP地址。 我本以为这很简单,但在搜索论坛后,似乎(难以置信)在Windows Phone 7中没有这种方法 - 不过,在Windows Phone 8中是否有改变? 感谢任何帮助。
我需要找到我的软件正在运行的手机的IP地址。 我本以为这很简单,但在搜索论坛后,似乎(难以置信)在Windows Phone 7中没有这种方法 - 不过,在Windows Phone 8中是否有改变? 感谢任何帮助。
注意,现在在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
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;
}