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
Not applicable.

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 Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2015 Microsoft