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

by Senthil Kumar | Monday, Oct 28, 2013 | 130 views

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

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

Unable to find the article that you are looking out for ? . Use the below Search box to search it

The following two tabs change content below.
Senthil Kumar is a Speaker , Blogger and Software Engineer from Bangalore. He is a Microsoft MVP in Client Development. He primarily works on .NET technologies like C# , ASP.NET MVC , Entity Framework , SQL Server etc. He is a Windows Phone Enthusiast and writes about Windows Phone and Mobile development at MobileOSGeek.com.
Like it? Share it!

1 Comment so far. Feel free to join this conversation.

  1. Jeremy Sinclair (@sinclairinator) November 1, 2013 at 4:35 pm - Reply

    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 Response