c# system.net.dns 通用应用程序 (W10) 不工作
c# system.net.dns universal app (W10) not working
我尝试使用 DNS 从 DNS 获取 IP 地址
IPAddress[] ip = Dns.GetHostAddresses("www.google.com");
通用 c# windows 应用程序。
但是它显示错误
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");
}
当您执行以下操作而不是 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 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
}
错误正确。 System.Net.Dns 在 .Net Framework for Windows Runtime 中不可用。
您可以改用 Windows 运行时 类:调用 Windows.Networking.Sockets.DatagramSocket.GetEndpointPairsAsync then examine the returned EndpointPairs
async Task ListEndpoints()
{
HostName host = new HostName("www.whosebug.com");
var eps = await DatagramSocket.GetEndpointPairsAsync(host, "80");
foreach(EndpointPair ep in eps)
{
Debug.WriteLine("EP {0} {1}",new object[] { ep.LocalHostName, ep.RemoteHostName });
}
}
我尝试使用 DNS 从 DNS 获取 IP 地址
IPAddress[] ip = Dns.GetHostAddresses("www.google.com");
通用 c# windows 应用程序。
但是它显示错误
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");
}
当您执行以下操作而不是 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 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
}
错误正确。 System.Net.Dns 在 .Net Framework for Windows Runtime 中不可用。
您可以改用 Windows 运行时 类:调用 Windows.Networking.Sockets.DatagramSocket.GetEndpointPairsAsync then examine the returned EndpointPairs
async Task ListEndpoints()
{
HostName host = new HostName("www.whosebug.com");
var eps = await DatagramSocket.GetEndpointPairsAsync(host, "80");
foreach(EndpointPair ep in eps)
{
Debug.WriteLine("EP {0} {1}",new object[] { ep.LocalHostName, ep.RemoteHostName });
}
}