How do I get the local machine name in C#?

System.Environment.MachineName

It works unless a machine name has more than 15 characters.


You should be able to use System.Environment.MachineName for this. It is a property that returns a string containing the netBIOS name of the computer:

http://msdn.microsoft.com/en-us/library/system.environment.machinename.aspx


From source

Four ways to get your local network/machine name:

string name = Environment.MachineName;
string name = System.Net.Dns.GetHostName();
string name = System.Windows.Forms.SystemInformation.ComputerName;
string name = System.Environment.GetEnvironmentVariable("COMPUTERNAME");

More information at: Difference between SystemInformation.ComputerName, Environment.MachineName, and Net.Dns.GetHostName

Tags:

C#

Dns

Hostname