This documentation is archived and is not being maintained.

Dns.GetHostName Method

Gets the host name of the local computer.

Namespace: System.Net
Assembly: System (in system.dll)

'Declaration
Public Shared Function GetHostName As String
'Usage
Dim returnValue As String

returnValue = Dns.GetHostName
public static String GetHostName ()
public static function GetHostName () : String

Return Value

A string that contains the DNS host name of the local computer.

Exception typeCondition

SocketException

An error is encountered when resolving the local host name.

The following example uses the GetHostName method to obtain the host name of the local computer.

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

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.get_Source());
        Console.WriteLine("Message : " + e.get_Message());
    }
    catch (System.Exception e) {
        Console.WriteLine("Exception caught!!!");
        Console.WriteLine("Source : " + e.get_Source());
        Console.WriteLine("Message : " + e.get_Message());
    }
} //DisplayLocalHostName

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Show: