2

I tried to get IP adress from DNS by using IPAddress[] ip = Dns.GetHostAddresses("www.google.com"); in univesal c# windows app.

But it shows me error Error CS0103 The name 'Dns' does not exist in the current context

I tried it in console app, it works perfectly. Namespace System.Net doesnt contain Dns in win 10 universal app. Could you tell me, where is problem or another solution?

My CODE

using System.Net;
public MainPage()
{
    this.InitializeComponent();
}

private void button_Click(object sender, RoutedEventArgs e)
{
    string zaznam = In.Text;
    IPAddress[] ip = Dns.GetHostAddresses("www.google.com");
}
Qefik
  • 23
  • 5
  • To get it work for me, I had to remove the www, so just ("google.com") .. Sorry, didn't read the bit that you were trying to get it work in windows app, – spacemonkeys Jan 12 '16 at 22:20
  • in windows universal app? It works for me in cosole app but not in universal :/ – Qefik Jan 12 '16 at 22:23

2 Answers2

0

what happens when you do the following instead ConsoleApp

IPHostEntry ipHostEntry = Dns.GetHostEntry("www.google.com");

or if there are more Ip addresses you can do the following

IPAddress[] ips;
ips = Dns.GetHostAddresses("www.google.com");
Console.WriteLine("GetHostAddresses({0}) returns:", "google.com");//if you are passing in a hostname inside a method change this to hostname variable
foreach (IPAddress ip in ips)
{
    Console.WriteLine("    {0}", ip);
}

Universal APP try the following below Microsoft.Phone.Net.NetworkInformation namespace

public void DnsLookup(string hostname)
{
    var endpoint = new DnsEndPoint(hostname, 0);
    DeviceNetworkInformation.ResolveHostNameAsync(endpoint, OnNameResolved, null);  
}

private void OnNameResolved(NameResolutionResult result)
{
    IPEndPoint[] endpoints = result.IPEndPoints;
    // Do something with your endpoints
}
MethodMan
  • 18,625
  • 6
  • 34
  • 52
  • thanks, but it isnt working, Microsoft.Phone.Net.NetworkInformation namespace is for WP, not universal app :/ I want to run it under W10(M) Sorry, I wrongly formule the question, I will edit it to be more obvious. – Qefik Jan 12 '16 at 23:09
0

The error is correct. System.Net.Dns is not available in the .Net Framework for Windows Runtime.

You can use the Windows Runtime classes instead: Call Windows.Networking.Sockets.DatagramSocket.GetEndpointPairsAsync then examine the returned EndpointPairs

async Task ListEndpoints()
{
    HostName host = new HostName("www.stackoverflow.com");
    var eps = await DatagramSocket.GetEndpointPairsAsync(host, "80");
    foreach(EndpointPair ep in eps)
    {
        Debug.WriteLine("EP {0} {1}",new object[] { ep.LocalHostName, ep.RemoteHostName });
    }
}
Rob Caplan - MSFT
  • 21,714
  • 3
  • 32
  • 54
  • That looks cool, but i cant really understand, why this code gives me this error: `Error CS4033 The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'.` I never used method like this. – Qefik Jan 13 '16 at 01:31
  • Exactly what the error says: you need to mark the containing method as async. This is an important and necessary pattern to understand when writing Universal apps in C#. See https://msdn.microsoft.com/en-us/library/hh191443.aspx for an overview – Rob Caplan - MSFT Jan 13 '16 at 01:48