Windows Phone 8中的IP地址

14

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

3个回答

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
IPAddress和GetHostNames在Windows Phone SDK中不存在。 - Omid Mafakher

3
当然有一种方法可以找到手机的IP地址。这里有一篇MSDN博客文章,解释了如何做到这一点:在Windows Phone Mango上找到自己的IP地址
我刚在我的Nokia Lumia 920(Windows Phone 8)上进行了测试,它完美地工作了。但是,这仅适用于使用组播IP的WiFi网络。

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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接