How to Get the IP Address of the Windows Phone 8 Programatically using C#?

The Windows Phone 8 SDK provides an easier way to identify the IP address of the phone from the Windows Phone App.

Below is a function that retrieves the IP Address of the Windows Phone 8 using C#? Note that there are possibilities of having multiple network interfaces on your Windows Phone. For example, the Windows Phone Emulator has 3 network interfaces as shown in the screenshot below. The code snippet tries to retrieve the first one which is added to the List.

How to Get the IP Address of the Windows Phone 8 Programatically using C#?

private void Button_Click_1(object sender, RoutedEventArgs e)
{
   IPAddress ObjIPAddress = GetIPAddress();
   string IPAddress = ObjIPAddress.ToString();
   MessageBox.Show(IPAddress);
}
// Function to get the IP Address from Windows Phone 8
public IPAddress GetIPAddress()
{
   List<string> IpAddress = new List<string>();
   var Hosts = Windows.Networking.Connectivity.NetworkInformation.GetHostNames().ToList();
   foreach (var Host in Hosts)
   {
      string IP = Host.DisplayName;
      IpAddress.Add(IP);
   }
   IPAddress address = IPAddress.Parse(IpAddress.Last());
   return address;
}

    1 Comment

  1. Wouldn’t it be easier to just to the following?

    public IPAddress GetIPAddress()
    {
    var Host= Windows.Networking.Connectivity.NetworkInformation.GetHostNames().Last();
    IPAddress address = IPAddress.Parse(Host.DisplayName);
    return address;
    }

    Since GetHostNames() returns a ReadOnlyList, there’s no reason to convert it to another type of list. Also… due to this function only returning the last element of the list, there’s no reason to loop through the other addresses.

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this post, you’ll learn about the Win32 Error “0x000019E5 – ERROR_COULD_NOT_RESIZE_LOG” that you get when debugging system erors in...
In this post, you’ll learn about the error “CO_E_DBERROR 0x8004E02B” that is returned when working with COM based APIs or...
In this post, you’ll learn about the Win32 Error “0x000019D0 – ERROR_LOG_BLOCK_VERSION” that you get when debugging system erors in...