如何在。net c#中获取计算机名


当前回答

试试这个:

string[] computer_name = System.Net.Dns.GetHostEntry(System.Web.HttpContext.Current.Request.ServerVariables["remote_addr"]).HostName.Split(new Char[] { '.' });
return computer_name[0].ToString();

其他回答

下面给出了获取机器名或计算机名的一些方法

方法1:

string MachineName1 = Environment.MachineName;

方法2:

string MachineName2 = System.Net.Dns.GetHostName();

方法三:

string MachineName3 = Request.ServerVariables["REMOTE_HOST"].ToString();

方法4:-

string MachineName4 = System.Environment.GetEnvironmentVariable("COMPUTERNAME");

欲了解更多,请参阅我的博客

试试这个:

string[] computer_name = System.Net.Dns.GetHostEntry(System.Web.HttpContext.Current.Request.ServerVariables["remote_addr"]).HostName.Split(new Char[] { '.' });
return computer_name[0].ToString();

用这一行简化一下

Environment.MachineName;

来自控制台或WinForms应用程序的System.Environment.MachineName。 来自web应用的HttpContext.Current.Server.MachineName System.Net.Dns.GetHostName()获取FQDN

参见如何在c# /中找到本地机器的FQDN。净吗?如果最后一个没有给你FQDN,而你需要它。

请参见“系统信息差异”。ComputerName,环境。MachineName和Net.Dns.GetHostName

2个更有用的方法: System.Environment。GetEnvironmentVariable(“ComputerName”)

System.Environment。GetEnvironmentVariable("ClientName")来获取用户PC的名称,如果他们通过Citrix XenApp或终端服务(又名RDS, RDP,远程桌面)连接