How to Get IP Address in Windows Phone 8

Let us see the procedure for programmatically getting the IP address in Windows Phone 8. Create a new project with a valid name. Once everything is ready our project will look as in the following screen.

page name

Now add one Button control to the triggers and show the IP address, change the button name to “Get lP”.

XAML Code

  1. <phone:PhoneApplicationPage  
  2.    x:Class="IPDemo.MainPage"  
  3.    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  4.    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  5.    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"  
  6.    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"  
  7.    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  8.    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  9.    mc:Ignorable="d"  
  10.    FontFamily="{StaticResource PhoneFontFamilyNormal}"  
  11.    FontSize="{StaticResource PhoneFontSizeNormal}"  
  12.    Foreground="{StaticResource PhoneForegroundBrush}"  
  13.    SupportedOrientations="Portrait" Orientation="Portrait"  
  14.    shell:SystemTray.IsVisible="True">  
  15.    <Grid x:Name="LayoutRoot" Background="Transparent">  
  16.       <Grid.RowDefinitions>  
  17.          <RowDefinition Height="Auto"/>  
  18.          <RowDefinition Height="*"/>  
  19.       </Grid.RowDefinitions>  
  20.       <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">  
  21.          <TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>  
  22.          <TextBlock Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>  
  23.       </StackPanel>  
  24.       <Grid x:Name="GetIP" Grid.Row="1" Margin="12,0,12,0">  
  25.          <Button Content="Get IP" HorizontalAlignment="Left" Margin="144,185,0,0" VerticalAlignment="Top" Click="Button_Click"/>  
  26.       </Grid>  
  27.    </Grid>  
  28. </phone:PhoneApplicationPage> 

Next we need to enable ID_CAP_NETWORKING. Open the WMAppManifest file and enable ID_CAP_NETWORKING as shown in the following screen.

WMAppManifest

Note that it is possible for there to be multiple network interfaces on your Windows Phone. For example, the Windows Phone Emulator has 3 network interfaces. The following code snippet tries to retrieve the first one in the List.

Now go to the code behind page.

C# Code

  1. private void Button_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.    IPAddress objIPaddress = GetIPs();  
  4.    MessageBox.Show(objIPaddress.ToString());  
  5. }  
  6. public IPAddress GetIPs()  
  7. {  
  8.    List<string> ipaddress = new List<string>();  
  9.    var hostlist = Windows.Networking.Connectivity.NetworkInformation.GetHostNames().ToList();  
  10.    foreach(var host in hostlist)  
  11.    {  
  12.       string ip = host.DisplayName;  
  13.       ipaddress.Add(ip);  
  14.    }  
  15.    IPAddress address = IPAddress.Parse(ipaddress.Last());  
  16.    return address;  

Now run your application. The output looks as in following screen:

output


Similar Articles