tags:

views:

1154

answers:

2

From what I have seen, in the MSDN documentation and in other questions here on SO, there are four ways to get the local machine name.

Environment.MachineName;
System.Net.Dns.GetHostName();
System.Windows.Forms.SystemInformation.ComputerName;
System.Environment.GetEnvironmentVariable(“COMPUTERNAME”);

Is there a differnece in what they methods will return or will they all return the exact same thing all of the time?

Note: I first saw the list in this post: http://stackoverflow.com/questions/662282/how-do-i-get-the-local-machine-name

A: 

Environment.MachineName : NetBIOS name of local computer read from registry

Dns.GetHostName : Gets host name of computer which refers to a domain name that has one or more associated IP adresses.

System.Windows.Forms.SystemInformation.ComputerName : same as Environment.MachineName, difference is you can call this from both web page and windows applications.Environment is used only Windows applications.

Environment.GetEnvironmentVariable method is used to retrieve environment variable from the current process.For more information , you may look at :
http://msdn.microsoft.com/en-us/library/system.environment.getenvironmentvariable.aspx

Myra
+2  A: 

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

System.Net.Dns.GetHostName() returns the computer's TCP/IP based hostname. By adding a domain suffix to the hostname you can resolve your computer's IP address across LANs / on the internet.

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

Dag