Dns.GetHostAddresses Method

Definition

Overloads

GetHostAddresses(String)

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

GetHostAddresses(String, AddressFamily)

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

GetHostAddresses(String)

Source:
Dns.cs
Source:
Dns.cs
Source:
Dns.cs

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

C#
public static System.Net.IPAddress[] GetHostAddresses(string hostNameOrAddress);

Parameters

hostNameOrAddress
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

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.

Examples

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

C#
public static void DoGetHostAddresses(string hostname)
{
    IPAddress[] addresses = Dns.GetHostAddresses(hostname);

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

    foreach (IPAddress address in addresses)
    {
        Console.WriteLine($"    {address}");
    }
}

Remarks

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.

Note

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

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

GetHostAddresses(String, AddressFamily)

Source:
Dns.cs
Source:
Dns.cs
Source:
Dns.cs

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

C#
public static System.Net.IPAddress[] GetHostAddresses(string hostNameOrAddress, System.Net.Sockets.AddressFamily family);

Parameters

hostNameOrAddress
String

The host name or IP address to resolve.

family
AddressFamily

The address family for which IPs should be retrieved. If Unspecified, retrieve all IPs regardless of address family.

Returns

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

Applies to

.NET 10 and other versions
Product Versions
.NET 6, 7, 8, 9, 10