Dns.GetHostName Method Home
This page is specific to:.NET Framework Version:1.12.03.03.54.0
.NET Framework Class Library
Dns.GetHostName Method

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 Language Filter 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

© 2009 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View