SystemInformation.ComputerName、Environment.MachineName 和 Net.Dns.GetHostName 之间的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1233217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between SystemInformation.ComputerName, Environment.MachineName, and Net.Dns.GetHostName
提问by epotter
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.
从我所看到的,在 MSDN 文档和其他关于 SO 的问题中,有四种方法可以获取本地机器名称。
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: How do I get the local machine Name?
注意:我第一次看到这个帖子中的列表: 如何获取本地机器名称?
采纳答案by EventHorizon
Environment.MachineNameand System.Windows.Forms.SystemInformation.ComputerNameare identical and returns the computer's NetBIOS name. This name is restricted to 15 characters and only visible on the LAN.
Environment.MachineName和System.Windows.Forms.SystemInformation.ComputerName是相同的并返回计算机的 NetBIOS 名称。此名称限制为 15 个字符,并且仅在 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.Net.Dns.GetHostName()返回计算机的基于 TCP/IP 的主机名。通过向主机名添加域后缀,您可以跨 LAN/在 Internet 上解析计算机的 IP 地址。
System.Environment.GetEnvironmentVariable("COMPUTERNAME")returns the computer name set during installation. NetBIOS and hostname are initially set to the same name.
System.Environment.GetEnvironmentVariable("COMPUTERNAME")返回安装期间设置的计算机名称。NetBIOS 和主机名最初设置为相同的名称。
回答by Myra
Environment.MachineName: NetBIOS name of local computer read from registry
Environment.MachineName: 从注册表中读取的本地计算机的 NetBIOS 名称
Dns.GetHostName: Gets host name of computer which refers to a domain name that has one or more associated IP adresses.
Dns.GetHostName:获取计算机的主机名,该主机名是指具有一个或多个关联 IP 地址的域名。
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.
System.Windows.Forms.SystemInformation.ComputerName: 与 Environment.MachineName 相同,不同之处在于您可以从网页和 Windows 应用程序中调用它。Environment 仅用于 Windows 应用程序。
Environment.GetEnvironmentVariablemethod 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
Environment.GetEnvironmentVariable方法用于从当前进程中检索环境变量。有关更多信息,您可以查看:http:
//msdn.microsoft.com/en-us/library/system.environment.getenvironmentvariable.aspx
回答by G?ran
There are some important differences between these methods. Say that you name your computer to "G?ransLilla人物987654321".
Environment.MachineNamewill then return G?RANSLILLA人物98. That is truncated and all upper case.
Dns.GetHostNamewill 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.
这些方法之间存在一些重要差异。假设您将计算机命名为“ G?ransLilla 人物987654321”。
Environment.MachineName然后将返回G?RANSLILLA 字符 98。那是截断的并且全部大写。
Dns.GetHostName将返回G?ransLilla??987654321。全长且大小写正确,但中文多字节字符已替换为“?”。瑞典语“?” 虽然被保留。
我知道获取 Windows 中指定的实际名称的唯一方法是使用 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());

