Skip to main content

Gets the full qualified domain name from the local machine (localhost) in CSharp.

        /// <summary>
        /// Gets the fully-qualified name of the current machine/host.
        /// </summary>
        /// <returns>Gets the fully-qualified name of the current machine/host,
        /// and returns the the <see cref="Environment.MachineName"/> if a <exception cref="System.Net.NetworkInformation.NetworkInformationException"></exception> is raised.
        /// </returns>
        private static string GetFullyQualifiedDomainName()
        {
            try
            {
                var ipProperties = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties();
                return string.IsNullOrWhiteSpace(ipProperties.DomainName) ?
                    ipProperties.HostName : string.Format("{0}.{1}", ipProperties.HostName,
                        ipProperties.DomainName);
            }
            catch (NetworkInformationException)
            {
                return Environment.MachineName;
            }
        }