This documentation is archived and is not being maintained.

Dns.Resolve Method

Resolves a DNS host name or IP address to an IPHostEntry instance.

[Visual Basic]
Public Shared Function Resolve( _
   ByVal hostName As String _
) As IPHostEntry
[C#]
public static IPHostEntry Resolve(
 string hostName
);
[C++]
public: static IPHostEntry* Resolve(
 String* hostName
);
[JScript]
public static function Resolve(
   hostName : String
) : IPHostEntry;

Parameters

hostName
A DNS-style host name or IP address.

Return Value

An IPHostEntry instance containing address information about the host specified in hostName.

Exceptions

Exception Type Condition
ArgumentNullException hostName is a null reference (Nothing in Visual Basic).
SocketException An error is encountered when resoloving hostName.
SecurityException The caller does not have permission to access DNS information.

Remarks

The Resolve method queries a DNS server for the IP address associated with a host name or IP address.

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.

Example

[Visual Basic, C#, C++] The following example uses the Resolve method to resolve an IP address to an IPHostEntry instance.

[Visual Basic] 
Public Sub DisplayHostAddress(hostString As [String])
    Try
        ' Call the Resolve method passing a DNS style host name or an IP address in 
        ' dotted-quad notation (for example, "www.contoso.com" or "207.46.131.199") to 
        ' obtain an IPHostEntry instance that contains address information for the 
        ' specified host.
        Dim hostInfo As IPHostEntry = Dns.Resolve(hostString)
        ' Get the IP address list that resolves to the host names contained in the Alias 
        ' property.
        Dim address As IPAddress() = hostInfo.AddressList
        ' Get the alias names of the addresses in the IP address list.
        Dim [alias] As [String]() = hostInfo.Aliases
        
        Console.WriteLine(("Host name : " + hostInfo.HostName))
        Console.WriteLine(ControlChars.Cr + "Aliases : ")
        Dim index As Integer
        For index = 0 To [alias].Length - 1
            Console.WriteLine([alias](index))
        Next index
        Console.WriteLine(ControlChars.Cr + "IP Address list :")

        For index = 0 To address.Length - 1
            Console.WriteLine(address(index))
        Next index
    Catch e As SocketException
        Console.WriteLine("SocketException caught!!!")
        Console.WriteLine(("Source : " + e.Source))
        Console.WriteLine(("Message : " + e.Message))
    Catch e As ArgumentNullException
        Console.WriteLine("ArgumentNullException caught!!!")
        Console.WriteLine(("Source : " + e.Source))
        Console.WriteLine(("Message : " + e.Message))
    Catch e As NullReferenceException
        Console.WriteLine("NullReferenceException caught!!!")
        Console.WriteLine(("Source : " + e.Source))
        Console.WriteLine(("Message : " + e.Message))
    Catch e As Exception
        Console.WriteLine("Exception caught!!!")
        Console.WriteLine(("Source : " + e.Source))
        Console.WriteLine(("Message : " + e.Message))
    End Try

[C#] 
try {
    IPHostEntry hostInfo = Dns.Resolve(hostString);
    // Get the IP address list that resolves to the host names contained in the 
    // Alias property.
    IPAddress[] address = hostInfo.AddressList;
    // Get the alias names of the addresses in the IP address list.
    String[] alias = hostInfo.Aliases;

    Console.WriteLine("Host name : " + hostInfo.HostName);
    Console.WriteLine("\nAliases : ");
    for(int index=0; index < alias.Length; index++) {
      Console.WriteLine(alias[index]);
    } 
    Console.WriteLine("\nIP Address list :");
    for(int index=0; index < address.Length; index++) {
       Console.WriteLine(address[index]);
    }
 }
 catch(SocketException e) 
 {
    Console.WriteLine("SocketException caught!!!");
    Console.WriteLine("Source : " + e.Source);
    Console.WriteLine("Message : " + e.Message);
 }
 catch(ArgumentNullException e)
 {
Console.WriteLine("ArgumentNullException caught!!!");
    Console.WriteLine("Source : " + e.Source);
    Console.WriteLine("Message : " + e.Message);
 }
 catch(NullReferenceException e)
 {
     Console.WriteLine("NullReferenceException caught!!!");
     Console.WriteLine("Source : " + e.Source);
     Console.WriteLine("Message : " + e.Message);
 }
 catch(Exception e)
 {
     Console.WriteLine("Exception caught!!!");
     Console.WriteLine("Source : " + e.Source);
     Console.WriteLine("Message : " + e.Message);
 }

[C++] 
try {
   IPHostEntry* hostInfo = Dns::Resolve(hostString);
   // Get the IP address list that resolves to the host names contained in the
   // Alias property.
   IPAddress* address[] = hostInfo->AddressList;
   // Get the alias names of the addresses in the IP address list.
   String*  alias[] = hostInfo->Aliases;

   Console::WriteLine(S"Host name : {0}", hostInfo->HostName);
   Console::WriteLine(S"\nAliases : ");
   for (int index=0; index < alias->Length; index++) {
      Console::WriteLine(alias->Item[index]);
   }
   Console::WriteLine(S"\nIP Address list :");
   for (int index=0; index < address->Length; index++) {
      Console::WriteLine(address->Item[index]);
   }
} catch (SocketException* e) {
   Console::WriteLine(S"SocketException caught!!!");
   Console::WriteLine(S"Source : {0}", e->Source);
   Console::WriteLine(S"Message : {0}", e->Message);
} catch (ArgumentNullException* e) {
   Console::WriteLine(S"ArgumentNullException caught!!!");
   Console::WriteLine(S"Source : {0}", e->Source);
   Console::WriteLine(S"Message : {0}", e->Message);
} catch (NullReferenceException* e) {
   Console::WriteLine(S"NullReferenceException caught!!!");
   Console::WriteLine(S"Source : {0}", e->Source);
   Console::WriteLine(S"Message : {0}", e->Message);
} catch (Exception* e) {
   Console::WriteLine(S"Exception caught!!!");
   Console::WriteLine(S"Source : {0}", e->Source);
   Console::WriteLine(S"Message : {0}", e->Message);
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

.NET Framework Security: 

See Also

Dns Class | Dns Members | System.Net Namespace

Show: