如何在C#中获取本地计算机的完全限定域名(FQDN)?
注意:此解决方案仅适用于针对.NET 2.0(及更高版本)框架的目标。
using System;
using System.Net;
using System.Net.NetworkInformation;
//...
public static string GetFQDN()
{
string domainName = IPGlobalProperties.GetIPGlobalProperties().DomainName;
string hostName = Dns.GetHostName();
domainName = "." + domainName;
if(!hostName.EndsWith(domainName)) // if hostname does not already include domain name
{
hostName += domainName; // add the domain name part
}
return hostName; // return the fully qualified name
}
更新
由于很多人评论说Sam的回答更简洁,所以我决定对答案添加一些注释。
最重要的是要注意,我提供的代码不等同于以下代码:
Dns.GetHostEntry("LocalHost").HostName
通常情况下,当计算机联网并且是域的一部分时,这两种方法通常会产生相同的结果,但在其他场景中,结果将有所不同。
一个输出不同的情况是当计算机不是域的一部分时。在这种情况下,Dns.GetHostEntry("LocalHost").HostName
将返回 localhost
,而上面的 GetFQDN()
方法将返回主机的 NETBIOS 名称。
如果查找计算机 FQDN 的目的是为了记录信息或生成报告,则此区别非常重要。大多数情况下,我在日志或报告中使用此方法,随后将其用于将信息映射回特定计算机。如果计算机未连接到网络,则 localhost
标识符是无用的,而名称提供所需的信息。
因此,对于每个用户来说,哪种方法更适合他们的应用程序取决于他们需要什么结果。但是说这个答案不够简明扼要是肤浅的。
看一个输出不同的示例:http://ideone.com/q4S4I0
对 Miky D 代码的稍微简化
public static string GetLocalhostFqdn()
{
var ipProperties = IPGlobalProperties.GetIPGlobalProperties();
return string.Format("{0}.{1}", ipProperties.HostName, ipProperties.DomainName);
}
.Trim(".")
以去除句末的句点(如果有的话)。 - David d C e Freitas这个技巧在这篇文章中有介绍。这种方法比被接受的答案更简洁,可能比得到第二多票数的答案更可靠。请注意,据我所知,这个方法没有使用NetBIOS名称,因此适用于互联网使用。
Dns.GetHostEntry("LocalHost").HostName
Dns.GetHostByName("LocalHost").HostName
Dns.GetHostEntry("").HostName
,而不是使用 Dns.GetHostEntry("LocalHost").HostName
。 - paulgutten这是 PowerShell 的代码,就为了好玩:
$ipProperties = [System.Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties()
"{0}.{1}" -f $ipProperties.HostName, $ipProperties.DomainName
对于Framework 1.1,只需要这样简单的操作:
System.Net.Dns.GetHostByName("localhost").HostName
然后删除机器NETBIOS名称,以仅检索domainName
GetHostByName("localhost")
被弃用。VS 2010建议我使用GetHostEntry("localhost")
代替,这个方法很好用。 - piedar您可以尝试以下方法:
return System.Net.Dns.GetHostEntry(Environment.MachineName).HostName;
这应该会给你当前本地机器的FQDN(或者你可以指定任何主机)。
public static string GetLocalhostFqdn()
{
var ipProperties = IPGlobalProperties.GetIPGlobalProperties();
return string.IsNullOrWhiteSpace(ipProperties.DomainName) ? ipProperties.HostName : string.Format("{0}.{1}", ipProperties.HostName, ipProperties.DomainName);
}
我将其作为一种选项来组合主机名和域名以构建报告,当未捕获到域名时添加通用文本以填充,这是客户的要求之一。
我使用C# 5.0、.Net 4.5.1进行了测试。
private static string GetHostnameAndDomainName()
{
// if No domain name return a generic string
string currentDomainName = IPGlobalProperties.GetIPGlobalProperties().DomainName ?? "nodomainname";
string hostName = Dns.GetHostName();
// check if current hostname does not contain domain name
if (!hostName.Contains(currentDomainName))
{
hostName = hostName + "." + currentDomainName;
}
return hostName.ToLower(); // Return combined hostname and domain in lowercase
}
return System.Net.Dns.GetHostEntry(Environment.MachineName).HostName;
System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).HostName
我测试的所有答案都没有提供我要找的DNS后缀。以下是我的解决方案。
public static string GetFqdn()
{
var networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
var ipprops = networkInterfaces.First().GetIPProperties();
var suffix = ipprops.DnsSuffix;
return $"{IPGlobalProperties.GetIPGlobalProperties().HostName}.{suffix}";
}
Dns.GetHostEntry("LocalHost").HostName
,我总是得到带有主域后缀的主机名(而不是 NetBIOS)。这与计算机是否属于域、DNS 服务器是否可达或网络是否连接无关。也许我没有理解您的解释,但结果符合我的预期。(机器:W2008R2;.net 4.0;NetBIOS 名称:TESTNAME-VERYLO 主机名:TESTNAME-VERYLONG) - marsh-wiggleEndsWith
检查对于以与域名相同的字母结尾的主机名(例如,在域OST中的主机MYHOST)是有问题的,应该改为EndsWith("." + domainName)
。 - user3391859hostName.
。应该进行一个!String.isNullorEmpty(domainName)
的检查。 - alaney