HostName HostName HostName HostName Class

Provides data for a hostname or an IP address.

Syntax

Declaration

public sealed class HostNamepublic sealed class HostNamePublic NotInheritable Class HostNamepublic sealed class HostName

Remarks

The HostName class is used to initialize and provide data for a hostname used in network apps. A HostName object can be used for a local hostname or a remote hostname used to establish a network connection.

The HostName object is used by many classes in other related namespaces for network apps. These include the following:


using Windows.Networking;
using Windows.Networking.Sockets;

var serverHost = new HostName("www.contoso.com");
var clientSocket = new Windows.Networking.Sockets.StreamSocket();

// Try to connect to the remote host
await clientSocket.ConnectAsync(serverHost, "http");
using Windows.Networking;
using Windows.Networking.Sockets;

HostName serverHost = new HostName("www.contoso.com");
StreamSocket clientSocket = new Windows.Networking.Sockets.StreamSocket();

// Try to connect to the remote host
await clientSocket.ConnectAsync(serverHost, "http");

using namespace Windows::Networking;
using namespace Windows::Networking::Sockets;

HostName^ serverHost= ref new HostName("www.contoso.com");
StreamSocket^ clientSocket = ref new StreamSocket();

// Try to connect to the remote host
clientSocket->ConnectAsync(serverHost, "http");

Constructors summary

Creates a new HostName object from a string that contains a hostname or an IP address.

Properties summary

Gets the canonical name for the HostName object.

Gets the display name for the HostName object.

Gets the IPInformation object for a local IP address assigned to a HostName object.

Gets the original string used to construct the HostName object.

Gets the HostNameType of the HostName object.

Methods summary

Compares two strings to determine if they represent the same hostname.

Determines whether the specified HostName object has an equivalent value to the current HostName object.

Returns a string that represents the HostName object.

Constructors

  • HostName(String)
    HostName(String)
    HostName(String)
    HostName(String)

    Creates a new HostName object from a string that contains a hostname or an IP address.

    public HostName(String hostName)public New(String hostName)Public Sub New(hostName As String)public HostName(String hostName)

    Parameters

    • hostName
      System.String
      System.String
      System.String
      System.String

      A string that contains a hostname or an IP address.

    Remarks

    This constructor creates a new HostName object. The hostName parameter can contain a hostname or an IP address. A HostName object can be used for a local hostname or a remote hostname.

    The hostName parameter can contain one of the following:

    • The name of a host that can be resolved by the Domain Name System (DNS) or by another namespace provider.
    • The name of a host that matches a string in the following file on the local computer: %WINDIR%\system32\drivers\etc\hosts
    • A string that contains an IPv4 or an IPv6 network address of the host. An IPv4 address string is in dotted-decimal notation ("192.168.1.111", for example). An IPv6 address is in hexadecimal notation ("21DA:00D3:0010:2F3B:02AA:00FF:FE28:9C5A", for example). If the hostName parameter is the "localhost" string, this represents all loopback addresses on the local computer.

    The GetHostNames() method in the Windows.Networking.Connectivity namespace can be used to retrieve a list of host names or IP addresses associated with the local machine. One of these hostnames or IP addresses can then be used for the hostName parameter.

    The constructor will fail if the hostName parameter is null or contains an empty string.

Properties

  • CanonicalName
    CanonicalName
    CanonicalName
    CanonicalName

    Gets the canonical name for the HostName object.

    public string CanonicalName { get; }public string CanonicalName { get; }Public ReadOnly Property CanonicalName As stringpublic string CanonicalName { get; }

    Property Value

    • string
      string
      string
      string

      The canonical name for the HostName object.

  • DisplayName
    DisplayName
    DisplayName
    DisplayName

    Gets the display name for the HostName object.

    public string DisplayName { get; }public string DisplayName { get; }Public ReadOnly Property DisplayName As stringpublic string DisplayName { get; }

    Property Value

    • string
      string
      string
      string

      The display name for the HostName object.

  • IPInformation
    IPInformation
    IPInformation
    IPInformation

    Gets the IPInformation object for a local IP address assigned to a HostName object.

    public IPInformation IPInformation { get; }public IPInformation IPInformation { get; }Public ReadOnly Property IPInformation As IPInformationpublic IPInformation IPInformation { get; }

    Property Value

    Remarks

    The IPInformation property is only set when the HostName object is a local IPv4 or IPv6 address returned by the GetHostNames() method. This property represents the IPInformation object for the local IP address. If the HostName object is not a local IPv4 or IPv6 address, this property will be null.

  • RawName
    RawName
    RawName
    RawName

    Gets the original string used to construct the HostName object.

    public string RawName { get; }public string RawName { get; }Public ReadOnly Property RawName As stringpublic string RawName { get; }

    Property Value

    • string
      string
      string
      string

      The original string used to construct the HostName object.

  • Type
    Type
    Type
    Type

    Gets the HostNameType of the HostName object.

    public HostNameType Type { get; }public HostNameType Type { get; }Public ReadOnly Property Type As HostNameTypepublic HostNameType Type { get; }

    Property Value

Methods

  • Compare(String, String)
    Compare(String, String)
    Compare(String, String)
    Compare(String, String)

    Compares two strings to determine if they represent the same hostname.

    public static int Compare(String value1, String value2)public static int Compare(String value1, String value2)Public Static Function Compare(value1 As String, value2 As String) As intpublic static int Compare(String value1, String value2)

    Parameters

    • value1
      System.String
      System.String
      System.String
      System.String

      A hostname or IP address.

    • value2
      System.String
      System.String
      System.String
      System.String

      A hostname or IP address.

    Returns

    • int
      int
      int
      int

      The return value indicates the lexicographic relation of value1 to value2. If the two parameters represent the same canonical hostname, then zero is returned. If value1 is less than value2, the return value is less than zero. If value1 is greater than value2, the return vale is greater than zero.

    Remarks

    The Compare(String, String) method is used to compare two strings to determine if the strings represent the same hostname. This method compares canonical names created from the value1 and value2 parameters to determine the value to return.

  • IsEqual(HostName)
    IsEqual(HostName)
    IsEqual(HostName)
    IsEqual(HostName)

    Determines whether the specified HostName object has an equivalent value to the current HostName object.

    public bool IsEqual(HostName hostName)public bool IsEqual(HostName hostName)Public Function IsEqual(hostName As HostName) As boolpublic bool IsEqual(HostName hostName)

    Parameters

    Returns

    • bool
      bool
      bool
      bool

      A Boolean value that indicates whether the specified HostName object is equal to the current HostName object.

    Remarks

    The Equals method determines whether the HostName object specified by the hostName parameter is the same instance of the current HostName object.

    The IsEqual(HostName) method determines whether the HostName object specified by the hostName parameter has an equivalent value to the current HostName object. Two HostName objects are considered to have the equivalent value if the CanonicalName property has the same value.

  • ToString()
    ToString()
    ToString()
    ToString()

    Returns a string that represents the HostName object.

    public string ToString()public string ToString()Public Function ToString() As stringpublic string ToString()

    Returns

    • string
      string
      string
      string

      A string that represents the HostName object.

Device family

Windows 10 (introduced v10.0.10240.0)

API contract

Windows.Foundation.UniversalApiContract (introduced v1)

Capabilities

privateNetworkClientServer
internetClient

Attributes

Windows.Foundation.Metadata.ActivatableAttribute
Windows.Foundation.Metadata.ContractVersionAttribute
Windows.Foundation.Metadata.DualApiPartitionAttribute
Windows.Foundation.Metadata.MarshalingBehaviorAttribute
Windows.Foundation.Metadata.StaticAttribute
Windows.Foundation.Metadata.ThreadingAttribute

Details

Assembly

Windows.Networking.dll