How can I get the current local hostname using C# or VB.NET?

C#vb.netWinforms

C# Problem Overview


I need to get the host name currently running the application. Any idea?

C# Solutions


Solution 1 - C#

Something to bear in mind is that System.Environment.MachineName; and System.Windows.Forms.SystemInformation.ComputerName; will give you the NETBIOS name of the machine (restricted to 15 characters).

If you want the full TCP/IP based host name you can use Dns.GetHostName():

string hostName = System.Net.Dns.GetHostName();

Or you can use:

System.Environment.GetEnvironmentVariable("COMPUTERNAME");

Which will return the full computer name set during installation.

Solution 2 - C#

Unless I am mistaken on what you want to do..

System.Environment.MachineName

Solution 3 - C#

To get fully qualified name, use:

 System.Net.Dns.GetHostEntry("").HostName

Solution 4 - C#

The My namespace contains many great "helper" functions like:

My.Computer.Name

Solution 5 - C#

System.Windows.Forms.SystemInformation.ComputerName;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBeginner_PalView Question on Stackoverflow
Solution 1 - C#djdd87View Answer on Stackoverflow
Solution 2 - C#WilView Answer on Stackoverflow
Solution 3 - C#DynamicView Answer on Stackoverflow
Solution 4 - C#NathanView Answer on Stackoverflow
Solution 5 - C#NobodyView Answer on Stackoverflow