HostEntry Class
System.Net
Represents the DNS (Domain Name System) entry for a host.
This class cannot be inherited.
public sealed class HostEntry
Note: This class is obsolete. For new development, use the
System.Net.Dns class to resolve host names.
Remarks
The HostEntry
class contains information about a host, including its host name and a list of IP addresses associated with it. You can use this class to programmatically access DNS information.
You can create an instance of the HostEntry
class by calling the Dns.GetHostEntry
method.
Members
Properties
-
HostName
public string HostName { get; }
Gets the primary DNS name of the host.
- Property Value
- A
string
that contains the DNS name of the host.
-
AddressList
public IPAddress[] AddressList { get; }
Gets an array of IPAddress
objects that are associated with this host.
- Property Value
- An array of
IPAddress
objects that contains the IP addresses associated with the host.
-
Aliases
public string[] Aliases { get; }
Gets an array of alias names for the host.
- Property Value
- A string array that contains the alias names for the host.
Methods
Example
// This example demonstrates how to resolve a host name and display its information.
// Note: This code uses the obsolete Dns.GetHostEntry overload for HostEntry.
// For new development, use the Dns.GetHostEntryAsync or Dns.GetHostAddresses methods.
using System;
using System.Net;
public class HostEntryExample
{
public static void Main(string[] args)
{
try
{
// Resolve the host name for "www.microsoft.com"
HostEntry hostInfo = Dns.GetHostEntry("www.microsoft.com");
Console.WriteLine($"Host Name: {hostInfo.HostName}");
Console.WriteLine("IP Addresses:");
foreach (IPAddress ip in hostInfo.AddressList)
{
Console.WriteLine($" - {ip}");
}
if (hostInfo.Aliases.Length > 0)
{
Console.WriteLine("Aliases:");
foreach (string alias in hostInfo.Aliases)
{
Console.WriteLine($" - {alias}");
}
}
}
catch (SocketException e)
{
Console.WriteLine($"SocketException caught: {e.Message}");
}
catch (Exception e)
{
Console.WriteLine($"An error occurred: {e.Message}");
}
}
}
Requirements
Namespace: System.Net
Assembly: System (in System.dll)