C# Code Snippet - Get local computer host name

C# Code Snippet - Get local computer host name

C# Code Snippet - Get local computer host name

(C-Sharp) C# code snippet get the host name from the local machine.

Bookmark:

C# Code Snippet - Get local computer host name

This .Net C# code snippet get the host name from the local machine. This function uses System.Net namespace to get local machine hostname. Modify the exception handling section to meet your project requirements.

/// <summary>
/// Gets host name of the local computer
/// </summary>
public string GetLocalHostName()
{
    return System.Net.Dns.GetHostName();
}


Here is a simple example showing how to use above function (GetLocalHostName) to display local machine host name.

Console.WriteLine("Local computer host name : " + GetLocalHostName());


C# Keywords Used:

  • DNS
  • GetHostName
  • Exception

Code Snippet Information:

  • Applies To: Visual Studio, .Net, C#, CLI, DNS, Domain Name System, GetHostName
  • Programming Language : C# (C-Sharp)

External Resources:

Leave a comment