c#system.net.dns通用应用程序(W10)不工作
本文关键字:W10 工作 应用程序 net dns c#system | 更新日期: 2023-09-27 18:30:05
我试图通过使用univesal c#windows应用程序中的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");
}
错误是正确的。System.Net.Dns在.Net Framework for Windows Runtime中不可用。
您可以使用Windows运行时类:调用Windows.Networking.Socket.DatagramSocket.GetEndpointPairsSync,然后检查返回的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 });
}
}
当您执行以下操作而不是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
}