Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the computer name in .NET

Tags:

c#

.net

People also ask

How do I find out what my computer is named?

Click on the Start button. Right-click on Computer. Select Properties. Under Computer name, domain, and workgroup settings you will find the computer name listed.

How do I find computer name using CMD?

In the window the window that appears on the bottom-left hand corner of your screen, type in cmd and click OK. The command prompt window will appear. In this window, type hostname and press Enter. The name of your computer will be displayed.

How do I find my computer name and domain?

Look up Your Computer's Domain Name. For Windows machines, click on the Start Menu, go to Control Panel, System and Security, then System. You'll see your computer's domain name at the bottom.


  • 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


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.


System.Environment.MachineName

Some methods are given below to get machine name or computer name

Method 1:-

string MachineName1 = Environment.MachineName;

Method 2:-

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

Method 3:-

string MachineName3 = Request.ServerVariables["REMOTE_HOST"].ToString();

Method 4:-

string MachineName4 = System.Environment.GetEnvironmentVariable("COMPUTERNAME");

For more see my blog


string name = System.Environment.MachineName;

Well there is one more way: Windows Management Instrumentation

using System.Management;

try
        {
            ManagementObjectSearcher searcher =
                new ManagementObjectSearcher("root\\CIMV2",
                "SELECT Name FROM Win32_ComputerSystem");

            foreach (ManagementObject queryObj in searcher.Get())
            {
                Console.WriteLine("-----------------------------------");
                Console.WriteLine("Win32_ComputerSystem instance");
                Console.WriteLine("-----------------------------------");
                Console.WriteLine("Name: {0}", queryObj["Name"]);
            }
        }
        catch (ManagementException e)
        {
            // exception handling
        }

MSDN

WMI

WMI Code creator

FAQs