C# 如何在 .NET 中获取计算机名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1768198/
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
How do I get the computer name in .NET
提问by
How do I get the computer name in .NET c#
如何在 .NET c# 中获取计算机名称
回答by Zach Johnson
System.Environment.MachineName
System.Environment.MachineName
Or, if you are using Winforms, you can use System.Windows.Forms.SystemInformation.ComputerName
, which returns exactly the same value as System.Environment.MachineName
.
或者,如果您使用的是 Winforms,则可以使用System.Windows.Forms.SystemInformation.ComputerName
,它返回与System.Environment.MachineName
.
回答by CesarGon
You can have access of the machine name using Environment.MachineName.
您可以使用Environment.MachineName访问机器名称。
回答by AlteredConcept
System.Environment.MachineName
回答by tvanfosson
System.Environment.MachineName
from a console or WinForms app.HttpContext.Current.Server.MachineName
from a web appSystem.Net.Dns.GetHostName()
to get the FQDN
System.Environment.MachineName
从控制台或 WinForms 应用程序。HttpContext.Current.Server.MachineName
从网络应用程序System.Net.Dns.GetHostName()
获取 FQDN
See How to find FQDN of local machine in C#/.NET ?if the last doesn't give you the FQDN and you need it.
请参阅如何在 C#/.NET 中查找本地计算机的 FQDN?如果最后一个没有给你 FQDN 而你需要它。
See details about Difference between SystemInformation.ComputerName, Environment.MachineName, and Net.Dns.GetHostName
详见SystemInformation.ComputerName、Environment.MachineName 和 Net.Dns.GetHostName 的区别
回答by Zach
string name = System.Environment.MachineName;
回答by PRR
Well there is one more way: Windows Management Instrumentation
那么还有一种方法:Windows Management Instrumentation
using System.Management;
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\CIMV2",
"SELECT Name FROM Win32_ComputerSystem");
foreach (ManagementObject queryObj in searcher.Get())
{
Console.WriteLine("-----------------------------------");
Console.WriteLine("Win32_ComputerSystem instance");
Console.WriteLine("-----------------------------------");
Console.WriteLine("Name: {0}", queryObj["Name"]);
}
}
catch (ManagementException e)
{
// exception handling
}
回答by A Ghazal
Try this:
尝试这个:
string[] computer_name = System.Net.Dns.GetHostEntry(System.Web.HttpContext.Current.Request.ServerVariables["remote_addr"]).HostName.Split(new Char[] { '.' });
return computer_name[0].ToString();
回答by Joe
I set the .InnerHtml of a <p>
bracket for my web project to the user's computer name doing the following:
我将<p>
Web 项目的括号的 .InnerHtml 设置为用户的计算机名称,执行以下操作:
HTML:
HTML:
<div class="col-md-4">
<h2>Your Computer Name Is</h2>
<p id="pcname" runat="server"></p>
<p>
<a class="btn btn-default" href="#">Learn more »</a>
</p>
</div>
C#:
C#:
using System;
using System.Web.UI;
namespace GetPCName {
public partial class _Default : Page {
protected void Page_Load(object sender, EventArgs e) {
pcname.InnerHtml = Environment.MachineName;
}
}
}
回答by JRrelyea
2 more helpful methods: System.Environment.GetEnvironmentVariable("ComputerName" )
2 个更有用的方法: System.Environment.GetEnvironmentVariable("ComputerName" )
System.Environment.GetEnvironmentVariable("ClientName" ) to get the name of the user's PC if they're connected via Citrix XenApp or Terminal Services (aka RDS, RDP, Remote Desktop)
System.Environment.GetEnvironmentVariable("ClientName") 获取通过 Citrix XenApp 或终端服务(又名 RDS、RDP、远程桌面)连接的用户 PC 的名称
回答by LEFOPHANA GODFREY
Try this one.
试试这个。
public static string GetFQDN()
{
string domainName = NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName;
string hostName = Dns.GetHostName();
string fqdn;
if (!hostName.Contains(domainName))
fqdn = hostName + "." +domainName;
else
fqdn = hostName;
return fqdn;
}