Export (0) Print
Expand All

Dns.GetHostName Method

Gets the host name of the local computer.

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

public:
static String^ 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:
   void DisplayLocalHostName()
   {
      try
      {
         // Get the local computer host name.
         String^ hostName = Dns::GetHostName();
         Console::WriteLine( "Computer name : {0}", hostName );
      }
      catch ( SocketException^ e ) 
      {
         Console::WriteLine( "SocketException 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 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

Community Additions

ADD
Show:
© 2014 Microsoft