Dns.GetHostAddresses(String) Method


Returns the Internet Protocol (IP) addresses for the specified host.

 static cli::array <System::Net::IPAddress ^> ^ GetHostAddresses(System::String ^ hostNameOrAddress);
public static System.Net.IPAddress[] GetHostAddresses (string hostNameOrAddress);
static member GetHostAddresses : string -> System.Net.IPAddress[]
Public Shared Function GetHostAddresses (hostNameOrAddress As String) As IPAddress()



The host name or IP address to resolve.


An array of type IPAddress that holds the IP addresses for the host that is specified by the hostNameOrAddress parameter.


hostNameOrAddress is null.

The length of hostNameOrAddress is greater than 255 characters.

An error is encountered when resolving hostNameOrAddress.

hostNameOrAddress is an invalid IP address.


The following code example uses the GetHostAddresses method to resolve an IP address to an array of type IPAddress.

    // Determine the Internet Protocol(IP) addresses for a host.
    static void DoGetHostAddress(String^ hostname)
        array<IPAddress^>^ addresses;
        addresses = Dns::GetHostAddresses(hostname);

        Console::WriteLine("GetHostAddresses({0}) returns:", hostname);
        for each (IPAddress^ address in addresses)
            Console::Write("{0} ", address);
public static void DoGetHostAddresses(string hostname)
    IPAddress[] addresses = Dns.GetHostAddresses(hostname);

    Console.WriteLine($"GetHostAddresses({hostname}) returns:");

    foreach (IPAddress address in addresses)
        Console.WriteLine($"    {address}");
Public Sub DoGetHostAddresses(hostName As String)

    Dim addresses As IPAddress() = Dns.GetHostAddresses(hostname)

    Console.WriteLine($"GetHostAddresses({hostname}) returns:")

    Dim index As Integer
    For index = 0 To addresses.Length - 1
        Console.WriteLine($"    {addresses(index)}")
    Next index

End Sub


The GetHostAddresses method queries the DNS subsystem for the IP addresses associated with a host name. If hostNameOrAddress is an IP address, this address is returned without querying the DNS server.

If an empty string is passed as the hostNameOrAddress argument, then this method returns the IPv4 and IPv6 addresses of the local host.

IPv6 addresses are filtered from the results of the GetHostAddresses method if the local computer does not have IPv6 installed. As a result, it is possible to get back an empty IPAddress instance if only IPv6 results were available for the hostNameOrAddress parameter.

This method is implemented using the underlying operating system's name resolution APIs (such as the Win32 API getaddrinfo on Windows, and equivalent APIs on other platforms). If a host is described in the hosts file, the IP address or addresses there will be returned without querying the DNS server.


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


for accessing DNS information. Associated enumeration: Unrestricted

Applies to