How to find FQDN of local machine in C#/.NET ?

2019-01-04 19:30发布

How can you get the FQDN of a local machine in C#?

9条回答
Viruses.
2楼-- · 2019-01-04 19:47

If you want to tidy it up, and handle exceptions, try this:

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;
        }
查看更多
时光不老,我们不散
3楼-- · 2019-01-04 19:48

This is covered by this article. This technique is more brief than the accepted answer and probably more reliable than the next most-voted answer. Note that as far as I understand, this doesn't use NetBIOS names, so it should be suitable for Internet use.

.NET 2.0+

Dns.GetHostEntry("LocalHost").HostName

.NET 1.0 - 1.1

Dns.GetHostByName("LocalHost").HostName
查看更多
▲ chillily
4楼-- · 2019-01-04 19:48

Here it is in PowerShell, for the heck of it:

$ipProperties = [System.Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties()
"{0}.{1}" -f $ipProperties.HostName, $ipProperties.DomainName
查看更多
乱世女痞
5楼-- · 2019-01-04 19:54

You can try the following:

return System.Net.Dns.GetHostEntry(Environment.MachineName).HostName;

This shoud give you the FQDN of the current local machine (or you can specify any host).

查看更多
Melony?
6楼-- · 2019-01-04 19:54

Used this as one of my options to combine host name and domain name for building a report, added the generic text to fill in when domain name was not captured, this was one of the customers requirements.

I tested this using 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
} 

Built using ideas from Miky Dinescu solution.

查看更多
Root(大扎)
7楼-- · 2019-01-04 20:00

A slight simplification of Miky D's code

    public static string GetLocalhostFqdn()
    {
        var ipProperties = IPGlobalProperties.GetIPGlobalProperties();
        return string.Format("{0}.{1}", ipProperties.HostName, ipProperties.DomainName);
    }
查看更多
登录 后发表回答