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


当前回答

用这一行简化一下

Environment.MachineName;

其他回答

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

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

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

System.Environment.MachineName

试试这个:

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

试试这个。

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;
}

用这一行简化一下

Environment.MachineName;