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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 20:39:03  来源:igfitidea点击:

How do I get the computer name in .NET

c#.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 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
        }

MSDN

MSDN

WMI

WMI

WMI Code creator

WMI 代码创建者

FAQs

常见问题

回答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 &raquo;</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;
}