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
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.