How to Get Computer Name in C#
-
Use the
Environment.MachineName
Property to Get the Computer Name inC#
-
Use the
GetHostName()
Method to Get the Computer Name inC#
-
Pass the
"COMPUTERNAME"
String to theSystem.Environment.GetEnvironmentVariable()
Method to Get the Computer Name inC#
-
Use the
SystemInformation.ComputerName
Property to Get the Computer Name inC#
There are different ways a developer can get the computer name in C#. The technical term for computer name is NetBIOS
name assigned by Windows to communicate over networks.
There are four different methods to find and get the computer name in C#.
Use the Environment.MachineName
Property to Get the Computer Name in C#
Environment
class is useful for getting various operating system-related information, including the computer name. MachineName
property is predefined in the Environment
class and can get the computer name.
Environment.MachineName
method returns a string that contains the computer name. It throws InvalidOperationException
when this property does not get the computer’s name.
using System;
class GetComputerName {
static public void Main() {
string ComputerName = Environment.MachineName;
Console.WriteLine("Computer Name: " + ComputerName);
}
}
The Environment
class is inherited from the System.Object
and provides information about the current platform of the executable C# code.
Use the GetHostName()
Method to Get the Computer Name in C#
The GetHostName()
method of the Dns
class is present in the System.Net
namespace. Use System.Net
namespace and define the GetHostName()
method to retrieve the computer name.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
namespace GetComputerName {
class ComputerName {
static void Main(string[] args) {
string Name = Dns.GetHostName();
Console.WriteLine("Computer Name: " + Name);
}
}
}
The NetBIOS
name of the computer is restricted to 15
characters, and this method helps get the complete computer name. When resolving the local computer name, it can throw a SocketException
error.
Pass the "COMPUTERNAME"
String to the System.Environment.GetEnvironmentVariable()
Method to Get the Computer Name in C#
The Environment.GetEnvironmentVariable()
method is useful for getting the computer name in C# for the current user. The environment variable contains the path of the Windows directory.
using System;
namespace GetComputerName {
class ComputerName {
static void Main(string[] args) {
string Name = Environment.GetEnvironmentVariable("COMPUTERNAME");
Console.WriteLine("Computer Name: " + Name);
Console.ReadLine();
}
}
}
GetEnvironmentVariable()
method is equivalent to calling the GetEnvironmentVariabble(String, EnvironmentVariableTarget)
method with a target
value of EnvironmentVariableTarget.Process
.
Environment variable names are not case-sensitive on Windows but can be extremely critical and sensitive on macOS and Linux.
Use the SystemInformation.ComputerName
Property to Get the Computer Name in C#
It gets the local computer’s NetBIOS
name, limited to only 15
characters. It gets the computer name of the current computer displayed to other users on a network.
using System;
using System.Drawing;
using System.Windows.Forms;
class ComputerName {
public static void Main() {
Console.WriteLine("Computer Name: " + SystemInformation.ComputerName);
}
}
ComputerName
method is only executable after inserting System.Drawing
and System.Windows.Forms
namespaces in your Solution Explorer
containing all your project files.
In conclusion, these are the four methods to get the computer name in C#. Each method is unique and can be executable under specific circumstances or conditions.
Hassan is a Software Engineer with a well-developed set of programming skills. He uses his knowledge and writing capabilities to produce interesting-to-read technical articles.
GitHub