System.Net.Dns Namespace
Provides classes for performing DNS (Domain Name System) name resolution.
Classes
Dns Class
Provides methods for performing DNS name resolution.
Methods
static HostEntry GetHostEntry(string hostNameOrAddress)
Resolves the specified DNS host name or IP address to an HostEntry object.
Parameters
| Name | Type | Description |
|---|---|---|
| hostNameOrAddress | string | The DNS name or IP address to resolve. |
Returns
HostEntry: A HostEntry object describing the network endpoint.
static IPAddress[] GetHostAddresses(string hostNameOrAddress)
Resolves a host name or IP address to an array of IPAddress objects.
Parameters
| Name | Type | Description |
|---|---|---|
| hostNameOrAddress | string | The DNS name or IP address to resolve. |
Returns
IPAddress[]: An array of IPAddress objects representing the IP addresses associated with the host. Returns an empty array if resolution fails.
static Task GetHostEntryAsync(string hostNameOrAddress)
Asynchronously resolves the specified DNS host name or IP address to an HostEntry object.
Parameters
| Name | Type | Description |
|---|---|---|
| hostNameOrAddress | string | The DNS name or IP address to resolve. |
Returns
Task<HostEntry>: A task that represents the asynchronous operation. The task result contains a HostEntry object describing the network endpoint.
Examples
GetHostEntry Example
using System;
using System.Net;
public class Example
{
public static void Main(string[] args)
{
try
{
IPHostEntry ipHostInfo = Dns.GetHostEntry("www.example.com");
Console.WriteLine($"Host name: {ipHostInfo.HostName}");
Console.WriteLine("Aliases:");
foreach (string alias in ipHostInfo.Aliases)
{
Console.WriteLine($"- {alias}");
}
Console.WriteLine("IP Addresses:");
foreach (IPAddress ipAddress in ipHostInfo.AddressList)
{
Console.WriteLine($"- {ipAddress}");
}
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
}
GetHostAddresses Example
using System;
using System.Net;
public class Example
{
public static void Main(string[] args)
{
try
{
IPAddress[] addresses = Dns.GetHostAddresses("www.microsoft.com");
Console.WriteLine("IP Addresses for www.microsoft.com:");
foreach (IPAddress address in addresses)
{
Console.WriteLine($"- {address}");
}
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
}
HostEntry Structure
Represents a DNS host, including its name, aliases, and IP addresses.
public class IPHostEntry
{
public string HostName { get; set; }
public string[] Aliases { get; set; }
public IPAddress[] AddressList { get; set; }
}
Properties:
HostName: The primary DNS name of the host.Aliases: An array of alternative DNS names for the host.AddressList: An array ofIPAddressobjects for the host.