2012-11-22 54 views
14

我需要找到我的軟件正在運行的手機的IP地址。我會認爲這是直接的,但已經搜索論壇似乎(難以置信的是)在Windows Phone 7中沒有這種方法 - 但是,這在Windows Phone 8中有所改變嗎?任何幫助,將不勝感激。windows phone 8中的IP地址

回答

3

當然有一種方法可以找到手機的IP地址。這裏是一個MSDN博客博客文章,它解釋瞭如何做到這一點:Finding Your Own IP Address On Windows Phone Mango

我剛剛在我的諾基亞Lumia 920(Windows Phone 8)上測試它,它工作得很好。但是,由於使用了多播IP,這隻適用於WiFi。

16

是的,現在可以在WP8中使用WP7所需的多播解決方案。請注意,你有你自己的手機上有多個網絡接口(例如,三個在我的WP8模擬器)

public static IPAddress Find() 
{ 
    List<string> ipAddresses = new List<string>(); 

    var hostnames = NetworkInformation.GetHostNames(); 
    foreach (var hn in hostnames) 
    { 
     if (hn.IPInformation != null) 
     { 
      string ipAddress = hn.DisplayName; 
      ipAddresses.Add(ipAddress); 
     } 
    } 

    IPAddress address = IPAddress.Parse(ipAddresses[0]); 
    return address; 
} 

HTH

+1

ip地址和GetHostNames劑量不存在於Windows手機SDK –

0

代號爲Windows RT

public static string GetIpAddress() 
{ 
     var address = ""; 
     var icp = NetworkInformation.GetInternetConnectionProfile(); 

     if (icp != null && icp.NetworkAdapter != null) 
     { 
      var hostname = 
       NetworkInformation.GetHostNames() 
        .SingleOrDefault(
         hn => 
         hn.IPInformation != null && hn.IPInformation.NetworkAdapter != null 
         && hn.IPInformation.NetworkAdapter.NetworkAdapterId 
         == icp.NetworkAdapter.NetworkAdapterId); 

      if (hostname != null) 
      { 
       address = hostname.CanonicalName; 
      } 
     } 
     return address; 
}