IPHostEntry Class
Namespace: System.Net
Assembly: System (in System.dll)
Summary
Provides a DNS host information structure.
The IPHostEntry
class contains information about a host, including its name, aliases, and IP addresses. You can use this class to store and retrieve DNS information.
Instances of IPHostEntry
are returned by the Dns.GetHostEntry
method, which performs a DNS lookup for the specified host name or IP address.
Syntax
public sealed class IPHostEntry
Members
The following table shows the members of the IPHostEntry
class.
Name | Description |
---|---|
Properties | Retrieves DNS host information. |
Methods | Provides functionality for manipulating or querying the host entry. |
Properties
Name | Description |
---|---|
AddressList IPAddress[] |
Gets or sets an array of IP addresses for the host. |
Aliases string[] |
Gets or sets an array of aliases for the host. |
HostName string |
Gets or sets the primary host name. |
Methods
Name | Description |
---|---|
ToString() |
Returns a string that represents the current object. |
Usage Example
The following example retrieves the IP address and host name for a given host using the Dns.GetHostEntry
method and displays the information.
using System; using System.Net; public class Example { public static void Main(string[] args) { try { // Replace with a valid host name or IP address string hostName = "www.microsoft.com"; IPHostEntry hostInfo = Dns.GetHostEntry(hostName); Console.WriteLine("Host Name: {0}", hostInfo.HostName); Console.WriteLine("IP Addresses:"); foreach (IPAddress ipAddress in hostInfo.AddressList) { Console.WriteLine(" {0}", ipAddress); } if (hostInfo.Aliases.Length > 0) { Console.WriteLine("Aliases:"); foreach (string alias in hostInfo.Aliases) { Console.WriteLine(" {0}", alias); } } } catch (Exception ex) { Console.WriteLine("An error occurred: {0}", ex.Message); } } }