IP address in windows phone 8
Asked Answered
C

3

14

i need to find the IP address of the phone my software is running on. I would have thought that is straight forward but having searched the forums it seems like (unbelievably enough) that there isn't a method for this in windows phone 7 - however, has this changed in windows phone 8? any help would be appreciated.

Casualty answered 22/11, 2012 at 5:40 Comment(0)
B
16

Yes this is now possible in WP8 without using the multicast solution required for WP7. Note that you will have multiple network interfaces on your phone (e.g. three on my WP8 Emulator)

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

Bren answered 17/12, 2012 at 9:42 Comment(1)
IPAddress and GetHostNames dose not exists in windows phone sdkMendenhall
N
3

Of course there is a way to find the phones IP address. Here is a MSDN blog blog article which explains how to do it: Finding Your Own IP Address On Windows Phone Mango

I've just tested it on my Nokia Lumia 920 (Windows Phone 8) and it works just fine. However, this just works on WiFi due to the used multicast IP.

Ned answered 7/12, 2012 at 23:56 Comment(0)
L
0

Code for 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;
}
Lichenin answered 3/1, 2017 at 7:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.