How do I get the computer name in .NET

Solution 1:

  • System.Environment.MachineName from a console or WinForms app.
  • HttpContext.Current.Server.MachineName from a web app
  • System.Net.Dns.GetHostName() to get the FQDN

See How to find FQDN of local machine in C#/.NET ? if the last doesn't give you the FQDN and you need it.

See details about Difference between SystemInformation.ComputerName, Environment.MachineName, and Net.Dns.GetHostName

Solution 2:

System.Environment.MachineName

Or, if you are using Winforms, you can use System.Windows.Forms.SystemInformation.ComputerName, which returns exactly the same value as System.Environment.MachineName.