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

This blog post will guide you through several effective methods to troubleshoot and resolve the issue of Microsoft Edge not...
Windows 11 offers a range of audio enhancements that can enrich your listening experience. These enhancements include features like virtual...
Windows 11 brings a fresh and visually stunning design to your desktop, and one of the standout features is the...