IPHostEntry.HostName Property
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets or sets the DNS name of the host.
public:
property System::String ^ HostName { System::String ^ get(); void set(System::String ^ value); };
public string HostName { get; set; }
member this.HostName : string with get, set
Public Property HostName As String
A string that contains the primary host name for the server.
The following example uses the HostName property to retrieve the primary host name.
void GetIpAddressList( String^ hostString )
{
try
{
// Get 'IPHostEntry' object containing information
// like host name, IP addresses, aliases for a host.
IPHostEntry^ hostInfo = Dns::GetHostByName( hostString );
Console::WriteLine( "Host name : {0}", hostInfo->HostName );
Console::WriteLine( "IP address List : " );
for ( int index = 0; index < hostInfo->AddressList->Length; index++ )
Console::WriteLine( hostInfo->AddressList[ index ] );
}
catch ( SocketException^ e )
{
Console::WriteLine( "SocketException caught!!!" );
Console::WriteLine( "Source : {0}", e->Source );
Console::WriteLine( "Message : {0}", e->Message );
}
catch ( ArgumentNullException^ e )
{
Console::WriteLine( "ArgumentNullException caught!!!" );
Console::WriteLine( "Source : {0}", e->Source );
Console::WriteLine( "Message : {0}", e->Message );
}
catch ( Exception^ e )
{
Console::WriteLine( "Exception caught!!!" );
Console::WriteLine( "Source : {0}", e->Source );
Console::WriteLine( "Message : {0}", e->Message );
}
}
public void GetIpAddressList(String hostString)
{
try
{
// Get 'IPHostEntry' object containing information like host name, IP addresses, aliases for a host.
IPHostEntry hostInfo = Dns.GetHostByName(hostString);
Console.WriteLine("Host name : " + hostInfo.HostName);
Console.WriteLine("IP address List : ");
for(int index=0; index < hostInfo.AddressList.Length; index++)
{
Console.WriteLine(hostInfo.AddressList[index]);
}
}
catch(SocketException e)
{
Console.WriteLine("SocketException caught!!!");
Console.WriteLine("Source : " + e.Source);
Console.WriteLine("Message : " + e.Message);
}
catch(ArgumentNullException e)
{
Console.WriteLine("ArgumentNullException caught!!!");
Console.WriteLine("Source : " + e.Source);
Console.WriteLine("Message : " + e.Message);
}
catch(Exception e)
{
Console.WriteLine("Exception caught!!!");
Console.WriteLine("Source : " + e.Source);
Console.WriteLine("Message : " + e.Message);
}
}
Public Sub GetIpAddressList(hostString As [String])
Try
' Get 'IPHostEntry' object which contains information like host name, IP addresses, aliases
' for specified url
Dim hostInfo As IPHostEntry = Dns.GetHostByName(hostString)
Console.WriteLine(("Host name : " + hostInfo.HostName))
Console.WriteLine("IP address List : ")
Dim index As Integer
For index = 0 To hostInfo.AddressList.Length - 1
Console.WriteLine(hostInfo.AddressList(index))
Next index
Catch e As SocketException
Console.WriteLine("SocketException caught!!!")
Console.WriteLine(("Source : " + e.Source))
Console.WriteLine(("Message : " + e.Message))
Catch e As ArgumentNullException
Console.WriteLine("ArgumentNullException caught!!!")
Console.WriteLine(("Source : " + e.Source))
Console.WriteLine(("Message : " + e.Message))
Catch e As Exception
Console.WriteLine("Exception caught!!!")
Console.WriteLine(("Source : " + e.Source))
Console.WriteLine(("Message : " + e.Message))
End Try
End Sub
The HostName property contains the primary host name for a server. If the DNS entry for the server defines additional aliases, they will be available in the Aliases property.
Product | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 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 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: