我尝试使用DNS从DNS获取IP地址
在{univeal c#windows app。{/ 1>}中IPAddress[] ip = Dns.GetHostAddresses("www.google.com");
但它显示错误
Error CS0103 The name 'Dns' does not exist in the current context
我在控制台应用程序中尝试过,它运行得很好。命名空间System.Net在win 10通用应用程序中不包含Dns。你能告诉我,问题或其他解决方案在哪里?
我的代码
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");
}
答案 0 :(得分:0)
当您执行以下操作而不是ConsoleApp
时会发生什么IPHostEntry ipHostEntry = Dns.GetHostEntry("www.google.com");
或者如果有更多的IP地址,您可以执行以下操作
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尝试以下Microsoft.Phone.Net.NetworkInformation命名空间
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
}
答案 1 :(得分:0)
错误是正确的。 System.Net.Dns在.Net Framework for Windows Runtime中不可用。
您可以改为使用Windows运行时类:调用Windows.Networking.Sockets。DatagramSocket.GetEndpointPairsAsync然后检查返回的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 });
}
}