Resolves a DNS host name or IP address to a System.Net.IPHostEntry instance.
- hostName
- A string containing a DNS-style host name or IP address.
A System.Net.IPHostEntry instance containing address information about the host specified in hostName .
Type Reason System.Net.DnsPermission Requires unrestricted permission for accessing DNS information. This method also asserts unrestricted DnsPermission . See System.Net.DnsPermission and System.Security.Permissions.PermissionState.Unrestricted.
Type Reason ArgumentNullException hostName is null . System.Net.Sockets.SocketException An error was encountered executing the DNS query . System.Security.SecurityException The caller does not have permission to access DNS information.
The Dns.Resolve(string) method queries a DNS server for the IP address associated with a host name or an IP address in dotted-quad notation.
When hostName is a DNS-style host name associated with multiple IP addresses, only the first IP address that resolves to that host name is returned.
The following example demonstrates the use of the Dns.Resolve(string) method.
C# Example
using System; using System.Net; public class DnsTest { public static void Main() { IPHostEntry hostInfo1 = Dns.Resolve("www.contoso.com"); DisplayHostInfo(hostInfo1); Console.WriteLine(); } public static void DisplayHostInfo(IPHostEntry hostInfo) { string[] aliases = hostInfo.Aliases; IPAddress[] addresses = hostInfo.AddressList; Console.WriteLine("The host name is: {0}", hostInfo.HostName); for(int x = 0; x < aliases.Length; x++) Console.WriteLine("Alias {0} == {1}", aliases[x], addresses[x]); } }The output is
The host name is: contoso.com
Alias www.contoso.com == 207.46.230.186
Namespace: System.Net
Assembly: System (in System.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0