Export (0) Print
Expand All

Dns.GetHostByName Method

NOTE: This method is now obsolete.

Gets the DNS information for the specified DNS host name.

Namespace: System.Net
Assembly: System (in system.dll)

[ObsoleteAttribute("GetHostByName is obsoleted for this type, please use GetHostEntry instead. http://go.microsoft.com/fwlink/?linkid=14202")] 
public static IPHostEntry GetHostByName (
	string hostName
)
/** @attribute ObsoleteAttribute("GetHostByName is obsoleted for this type, please use GetHostEntry instead. http://go.microsoft.com/fwlink/?linkid=14202") */ 
public static IPHostEntry GetHostByName (
	String hostName
)
ObsoleteAttribute("GetHostByName is obsoleted for this type, please use GetHostEntry instead. http://go.microsoft.com/fwlink/?linkid=14202") 
public static function GetHostByName (
	hostName : String
) : IPHostEntry

Parameters

hostName

The DNS name of the host.

Return Value

An IPHostEntry object that contains host information for the address specified in hostName.

Exception typeCondition

ArgumentNullException

hostName is a null reference (Nothing in Visual Basic).

ArgumentOutOfRangeException

The length of hostName is greater than 126 characters.

SocketException

An error is encountered when resolving hostName.

The GetHostByName method queries the Internet DNS server for host information. If you pass an empty string as the host name, this method retrieves the standard host name for the local computer.

For asynchronous access to DNS information, use the BeginGetHostByName and EndGetHostByName methods.

NoteNote

This member emits trace information when you enable network tracing in your application. For more information, see Network Tracing.

The following example uses the GetHostByName method to get the DNS information for the specified DNS host name.

 try 
 {
    IPHostEntry hostInfo = Dns.GetHostByName(hostName);
    // 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(Exception e)
 {
     Console.WriteLine("Exception caught!!!");
     Console.WriteLine("Source : " + e.Source);
     Console.WriteLine("Message : " + e.Message);
 }

try {
    IPHostEntry hostInfo = Dns.GetHostByName(hostName);

    // Get the IP address list that resolves to the host 
    // names contained in the  Alias property.
    IPAddress address[] = hostInfo.get_AddressList();

    // Get the alias names of the addresses in the IP address list.
    String alias[] = hostInfo.get_Aliases();

    Console.WriteLine("Host name : " + hostInfo.get_HostName());
    Console.WriteLine("\nAliases : ");
    for (int index = 0; index < alias.length; index++) {
        Console.WriteLine(alias.get_Item(index));
    }
    Console.WriteLine("\nIP address list : ");
    for (int index = 0; index < address.length; index++) {
        Console.WriteLine(address.get_Item(index));
    }
}
catch (SocketException e) {
    Console.WriteLine("SocketException caught!!!");
    Console.WriteLine("Source : " + e.get_Source());
    Console.WriteLine("Message : " + e.get_Message());
}
catch (ArgumentNullException e) {
    Console.WriteLine("ArgumentNullException caught!!!");
    Console.WriteLine("Source : " + e.get_Source());
    Console.WriteLine("Message : " + e.get_Message());
}
catch (System.Exception e) {
    Console.WriteLine("Exception caught!!!");
    Console.WriteLine("Source : " + e.get_Source());
    Console.WriteLine("Message : " + e.get_Message());
}

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 1.0, 1.1
Obsolete (compiler warning) in 2.0

.NET Compact Framework

Supported in: 1.0
Obsolete (compiler warning) in 2.0

Community Additions

ADD
Show:
© 2014 Microsoft