如何在C#/.NET中查找本地计算机的FQDN?

95

如何在C#中获取本地计算机的完全限定域名(FQDN)?

13个回答

0
我处理有关FQ主机名/主机名/NetBIOS机器名/域名的所有情况的方法集合。
    /// <summary>
    /// Get the full qualified hostname
    /// </summary>
    /// <param name="throwOnMissingDomainName"></param>
    /// <returns></returns>
    public static string GetMachineFQHostName(bool throwOnMissingDomainName = false)
    {
        string domainName = GetMachineFQDomainName();
        string hostName = GetMachineHostName();

        if (string.IsNullOrEmpty(domainName) && throwOnMissingDomainName) throw new Exception($"Missing domain name on machine: { hostName }");
        else if (string.IsNullOrEmpty(domainName)) return hostName;
        //<----------

        return $"{ hostName }.{ domainName }";
    }


    /// <summary>
    /// Get the NetBIOS name of the local machine
    /// </summary>
    /// <returns></returns>
    public static string GetMachineName()
    {
        return Environment.MachineName;
    }

    /// <summary>
    /// Get the Hostname from the local machine which differs from the NetBIOS name when 
    /// longer than 15 characters
    /// </summary>
    /// <returns></returns>
    public static string GetMachineHostName()
    {
        /// I have been told that GetHostName() may return the FQName. Never seen that, but better safe than sorry ....
        string hostNameRaw = System.Net.Dns.GetHostName();
        return hostNameRaw.Split('.')[0];
    }

    /// <summary>
    /// Check if hostname and NetBIOS name are equal
    /// </summary>
    /// <returns></returns>
    public static bool AreHostNameAndNetBIOSNameEqual()
    {
        return GetMachineHostName().Equals(GetMachineName(), StringComparison.OrdinalIgnoreCase);
    }

    /// <summary>
    /// Get the domain name without the hostname
    /// </summary>
    /// <returns></returns>
    public static string GetMachineFQDomainName()
    {
        return IPGlobalProperties.GetIPGlobalProperties().DomainName;
    }

0
我使用了这种方法:
private static string GetLocalhostFQDN()
{
    var ipProperties = IPGlobalProperties.GetIPGlobalProperties();
    return $"{ipProperties.HostName}.{ipProperties.DomainName}";
}

-9
如果你想整理它并处理异常,请尝试这个:
public static string GetLocalhostFQDN()
        {
            string domainName = string.Empty;
            try
            {
                domainName = NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName;
            }
            catch
            {
            }
            string fqdn = "localhost";
            try
            {
                fqdn = System.Net.Dns.GetHostName();
                if (!string.IsNullOrEmpty(domainName))
                {
                    if (!fqdn.ToLowerInvariant().EndsWith("." + domainName.ToLowerInvariant()))
                    {
                        fqdn += "." + domainName;
                    }
                }
            }
            catch
            {
            }
            return fqdn;
        }

2
"并处理异常" - 失败。 - Jörgen Sigvardsson
你认为仅凭catch就能处理异常! - Saher Ahwal
不,我认为如何处理异常是你需要自己决定的事情。如果你无法取回域名,你会如何处理呢? - Roger Willcocks
4
记录错误报告并警告用户出现了问题,而不是向客户端代码提供错误信息。 - dahvyd

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接