如何在 C# 中获取本地计算机名称?
如何获取本地机器名称?
How do I get the local machine name?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何获取本地机器名称?
How do I get the local machine name?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
System.Environment.MachineName
除非计算机名称超过 15 个字符。
System.Environment.MachineName
It works unless a machine name has more than 15 characters.
从
来源
四种方法获取您的本地网络/计算机名称:
更多信息位于:
SystemInformation.ComputerName、Environment.MachineName 和 Net.Dns.GetHostName 之间的差异
From
source
Four ways to get your local network/machine name:
More information at:
Difference between SystemInformation.ComputerName, Environment.MachineName, and Net.Dns.GetHostName
您应该能够使用 System.Environment.MachineName 来实现此目的。 它是一个返回包含计算机的 netBIOS 名称的字符串的属性:
http://msdn.microsoft.com/en-us/library/system.environment.machinename.aspx
You should be able to use
System.Environment.MachineName
for this. It is a property that returns a string containing the netBIOS name of the computer:http://msdn.microsoft.com/en-us/library/system.environment.machinename.aspx
如果您想要本地计算机的 FQDN(完全限定域名),可以使用
System.Net.Dns.GetHostEntry("localhost").HostName
其他方法只会返回本地名称,没有任何特定于域的信息。 例如,对于计算机
myComp.myDomain.com
,前面的方法将返回myComp
,而GetHostEntry
方法将返回myComp .myDomain.com
If you want the FQDN (fully qualified domain name) of the local computer, you can use
System.Net.Dns.GetHostEntry("localhost").HostName
The other methods will only return the local name, without any domain specific info. For instance, for the computer
myComp.myDomain.com
, the previous methods will returnmyComp
, whereas theGetHostEntry
method will returnmyComp.myDomain.com
我的计算机名称超过 15 个字符,因此我使用 hostname.exe 获取全长名称:
My computer name is more than 15 chars, so i use hostname.exe to get full length name: