2012-11-21 5 views
6

मैं मेट्रो ऐप क्लास लाइब्रेरी में विंडोज फॉर्म क्लास लाइब्रेरी माइग्रेट कर रहा हूं। कि एक कोड ब्लॉक जो नीचे से होस्ट नाम IPAddress देता है,सी # विंडोज 8 मेट्रो ऐप में होस्टनाम से आईपी एड्रेस कैसे प्राप्त करें?

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

वहाँ उदाहरण के लिए:

पता: talk.google.com

IPADDRESS: xx.xxx.xxx.xx

लेकिन मैंने देखा है कि मेट्रो ऐप सिस्टम.Net में IPHostEntry या Dns या IPAddress नहीं है। ।

यदि कोई जानता है तो कृपया मुझे विंडोज 8 मेट्रो ऐप में इनके लिए प्रतिस्थापन बताएं।

+0

यहाँ एक सुंदर गोल चक्कर तरीका है: http://stackoverflow.com/questions/11216625/how-to-resolve-a-hostname-to-an-ip-address- इन-मेट्रो-विनर्ट –

+0

Dns.GetHostByName () आज़माएं; – GiantHornet

+0

मैंने कोशिश की है, यह ConnectAsync विधि से बाहर निकलता है। । । –

उत्तर

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