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

寵の児 提交于 2019-11-27 19:13:59
EventHorizon

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.

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

Göran

There are some important differences between these methods. Say that you name your computer to "GöransLilla人物987654321".
Environment.MachineName will then return GÖRANSLILLA人物98. That is truncated and all upper case.
Dns.GetHostName will return GöransLilla??987654321. Full length and correct casing but the Chinese multibyte characters have been replaced with '?'. The Swedish 'ö' is kept though.

The only way I know of getting the actual name as specified in Windows is with pinvoke.

[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern bool GetComputerNameEx(COMPUTER_NAME_FORMAT NameType, System.Text.StringBuilder lpBuffer, ref uint lpnSize);

System.Text.StringBuilder nameBuilder = new System.Text.StringBuilder(260);
uint size = 260;
bool success = GetComputerNameEx(COMPUTER_NAME_FORMAT.ComputerNameDnsHostname, nameBuilder, ref size);
Console.WriteLine(nameBuilder.ToString());
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!