How to get Local Machine Name in C# or VB.Net etc.

In .Net Framework, Computer Name (local network/machine name) can be found through following ways for ASP.Net (Web Forms & MVC) and Windows Application:


string machineName1 = Environment.MachineName;
string machineName2 = System.Environment.GetEnvironmentVariable("COMPUTERNAME");
string hostName= System.Net.Dns.GetHostName();
string computerName = System.Windows.Forms.SystemInformation.ComputerName;

What is the Difference?

The local machine name can be received through several namespace class function and properties with some differences in this network programming:

MachineName and ComputerName

Environment.MachineName and System.Windows.Forms.SystemInformation.ComputerName returns the computer's NetBIOS name, restricted to 15 characters and visible only on the LAN and Windows Forms.

GetHostName

System.Net.Dns.GetHostName() returns TCP/IP based host name of computer. By adding a domain suffix to the host name you can resolve your computer's IP address across the local network including LAN and internet. This is for Web Forms and MVC etc.

GetEnvironmentVariable

System.Environment.GetEnvironmentVariable("COMPUTERNAME") returns the computer name set during installation. NetBIOS and host name are initially set to the same name.

Posted Status in Programming
Login InOR Register