Gets the host name of the local computer.
[Visual Basic]
Public Shared Function GetHostName() As String
[C#]
public static string GetHostName();
[C++]
public: static String* GetHostName();
[JScript]
public static function GetHostName() : String;
Return Value
A string containing the DNS host name of the local computer.
Exceptions
| Exception Type | Condition |
| SocketException | An error is encountered when resolving the local host name. |
| SecurityException | The caller does not have permission to access DNS information. |
Example
[Visual Basic, C#, C++] The following example uses the GetHostName method to obtain the host name of the local computer.
[Visual Basic]
Public Sub DisplayLocalHostName()
Try
' Get the local computer host name.
Dim hostName As [String] = Dns.GetHostName()
Console.WriteLine(("Computer name :" + hostName))
Catch e As SocketException
Console.WriteLine("SocketException 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
[C#]
public void DisplayLocalHostName()
{
try {
// Get the local computer host name.
String hostName = Dns.GetHostName();
Console.WriteLine("Computer name :" + hostName);
}
catch(SocketException e) {
Console.WriteLine("SocketException 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);
}
}
[C++]
public:
void DisplayLocalHostName()
{
try {
// Get the local computer host name.
String* hostName = Dns::GetHostName();
Console::WriteLine(S"Computer name : {0}", hostName);
} catch (SocketException* e) {
Console::WriteLine(S"SocketException caught!!!");
Console::WriteLine(S"Source : {0}", e->Source);
Console::WriteLine(S"Message : {0}", e->Message);
} catch (Exception* e) {
Console::WriteLine(S"Exception caught!!!");
Console::WriteLine(S"Source : {0}", e->Source);
Console::WriteLine(S"Message : {0}", e->Message);
}
}
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard
.NET Framework Security:
See Also
Dns Class | Dns Members | System.Net Namespace