2012-11-21 8 views
6

Mam migrację biblioteki klas formularza Windows do biblioteki klas aplikacji aplikacji Metro. W tym że nie jest to kod blok, który daje IPaddress z nazwy hosta, poniżej,Jak uzyskać adres IP od nazwy hosta w oknach C# 8 aplikacji Metro?

IPHostEntry ipHostInfo = Dns.GetHostEntry(Address); 
IPAddress ipAddress = ipHostInfo.AddressList[0];// IPAddress.Parse(address); 
IPEndPoint endPoint = new IPEndPoint(ipAddress, Port); 

np

Adres: talk.google.com

ipAddress: xx.xxx.xxx.xx

ale widziałem że w Metro App System.Net nie ma IPHostEntry lub Dns ani IPAddress. .

Jeśli ktokolwiek wie, proszę powiedz mi o zastąpienie tych w aplikacji Windows 8 metra.

+0

Oto bardzo okrężną drogą: http://stackoverflow.com/questions/11216625/how-to-resolve-a-hostname-to-an-ip-address- in-metro-winrt –

+0

wypróbuj Dns.GetHostByName (); – GiantHornet

+0

Próbowałem już, to wychodzi z metody ConnectAsync. . . –

Odpowiedz

2
using System.Threading.Tasks; 

public async static Task<string> ResolveDNS(string remoteHostName) 
    { 
     if (string.IsNullOrEmpty(remoteHostName)) 
      return string.Empty; 

     string ipAddress = string.Empty; 

     try 
     { 
      IReadOnlyList<EndpointPair> data = 
       await DatagramSocket.GetEndpointPairsAsync(new HostName(remoteHostName), "0"); 

      if (data != null && data.Count > 0) 
      { 
       foreach (EndpointPair item in data) 
       { 
        if (item != null && item.RemoteHostName != null && 
            item.RemoteHostName.Type == HostNameType.Ipv4) 
        { 
         return item.RemoteHostName.CanonicalName; 
        } 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
      ipAddress = ex.Message; 
     } 

     return ipAddress; 
    } 
Powiązane problemy