如何在 C# 中获取本地计算机名称?

发布于 2024-07-15 12:01:41 字数 18 浏览 3 评论 0原文

如何获取本地机器名称?

How do I get the local machine name?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

秉烛思 2024-07-22 12:01:42


来源

四种方法获取您的本地网络/计算机名称:

string name = Environment.MachineName;
string name = System.Net.Dns.GetHostName();
string name = System.Windows.Forms.SystemInformation.ComputerName;
string name = System.Environment.GetEnvironmentVariable("COMPUTERNAME");

更多信息位于:
SystemInformation.ComputerName、Environment.MachineName 和 Net.Dns.GetHostName 之间的差异

From
source

Four ways to get your local network/machine name:

string name = Environment.MachineName;
string name = System.Net.Dns.GetHostName();
string name = System.Windows.Forms.SystemInformation.ComputerName;
string name = System.Environment.GetEnvironmentVariable("COMPUTERNAME");

More information at:
Difference between SystemInformation.ComputerName, Environment.MachineName, and Net.Dns.GetHostName

吃→可爱长大的 2024-07-22 12:01:42

您应该能够使用 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

第七度阳光i 2024-07-22 12:01:42

如果您想要本地计算机的 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 return myComp, whereas the GetHostEntry method will return myComp.myDomain.com

梦过后 2024-07-22 12:01:42

我的计算机名称超过 15 个字符,因此我使用 hostname.exe 获取全长名称:

Process proc = new Process();
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.FileName = "c:/windows/system32/hostname.exe";
proc.Start();
var hostName = proc.StandardOutput.ReadLine();

My computer name is more than 15 chars, so i use hostname.exe to get full length name:

Process proc = new Process();
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.FileName = "c:/windows/system32/hostname.exe";
proc.Start();
var hostName = proc.StandardOutput.ReadLine();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文