Dns.GetHostAddresses(String) Dns.GetHostAddresses(String) Dns.GetHostAddresses(String) Dns.GetHostAddresses(String) Method

Definition

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

public:
 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()
Parameters
hostNameOrAddress
String String String String

The host name or IP address to resolve.

Returns

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

Exceptions

An error is encountered when resolving hostNameOrAddress.

hostNameOrAddress is an invalid IP address.

Examples

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.
public:
    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);
        }
        Console::WriteLine("");
   }
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

Remarks

The GetHostAddresses method queries a DNS server 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 where available for the hostNameOrAddress.parameter.

Note

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

Security

DnsPermission
for accessing DNS information. Associated enumeration: Unrestricted

Applies to