How to Find Fqdn of Local MAChine in C#/.Net

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

NOTE: This solution only works when targeting the .NET 2.0 (and newer) frameworks.

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
}

UPDATE

Since a lot of people have commented that Sam's Answer is more concise I've decided to add some comments to the answer.

The most important thing to note is that the code I gave is not equivalent to the following code:

Dns.GetHostEntry("LocalHost").HostName

While in the general case when the machine is networked and part of a domain, both methods will generally produce the same result, in other scenarios the results will differ.

A scenario where the output will be different is when the machine is not part of a domain. In this case, the Dns.GetHostEntry("LocalHost").HostName will return localhost while the GetFQDN() method above will return the NETBIOS name of the host.

This distinction is important when the purpose of finding the machine FQDN is to log information, or generate a report. Most of the time I've used this method in logs or reports that are subsequently used to map information back to a specific machine. If the machines are not networked, the localhost identifier is useless, whereas the name gives the needed information.

So ultimately it's up to each user which method is better suited for their application, depending on what result they need. But to say that this answer is wrong for not being concise enough is superficial at best.

See an example where the output will be different: http://ideone.com/q4S4I0

How do I find the fully qualified hostname of my machine in C#?

You may be able to get the whole DNS string like this:

System.Net.Dns.GetHostEntry("").HostName

We don't have full fledged DNS names where I work, but it does give me a three level faux domain name instead of just the hostname.

Edit 2011/03/17: Incorporated changes suggested by mark below.

How to get full host name in C#?

You can use System.Net.Dns.GetHostEntry:

var fullName = System.Net.Dns.GetHostEntry(string.Empty).HostName;

If an empty string is passed as the hostNameOrAddress argument, then this method returns the IPv4 and IPv6 addresses of the local host.

Getting FQDN for current user in c#

After some research, this gets the job done:

public static class GetUserNameExUtil
{
#region Interop Definitions
public enum EXTENDED_NAME_FORMAT
{
NameUnknown = 0,
NameFullyQualifiedDN = 1,
NameSamCompatible = 2,
NameDisplay = 3,
NameUniqueId = 6,
NameCanonical = 7,
NameUserPrincipal = 8,
NameCanonicalEx = 9,
NameServicePrincipal = 10,
NameDnsDomain = 12,
}
[System.Runtime.InteropServices.DllImport("secur32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
public static extern int GetUserNameEx(int nameFormat, StringBuilder userName, ref int userNameSize);
#endregion

public static string GetUserName(EXTENDED_NAME_FORMAT nameFormat)
{
if (Environment.OSVersion.Platform != PlatformID.Win32NT)
{
return null;
}

StringBuilder userName = new StringBuilder(1024);
int userNameSize = userName.Capacity;
if (GetUserNameEx((int)nameFormat, userName, ref userNameSize) != 0)
{
string[] nameParts = userName.ToString().Split('\\');
return nameParts[0];
}

return null;
}
public static string GetUserFullName()
{
return GetUserName(EXTENDED_NAME_FORMAT.NameDnsDomain);
}
}

Get FQDN in C# running on Mono

To do this, you can pretty much do the same you'd do in C on a UNIX system, which is to retrieve the hostname with gethostname() and then use a DNS lookup to find the canonical network name for the host. Luckily, System.Net has ready-made calls for this. The following code should work on both OS X and Linux (in fact, on Linux it is more or less what hostname --fqdn does):

using System;
using System.Net;

class Program {
static void Main() {
// Step 1: Get the host name
var hostname = Dns.GetHostName();
// Step 2: Perform a DNS lookup.
// Note that the lookup is not guaranteed to succeed, especially
// if the system is misconfigured. On the other hand, if that
// happens, you probably can't connect to the host by name, anyway.
var hostinfo = Dns.GetHostEntry(hostname);
// Step 3: Retrieve the canonical name.
var fqdn = hostinfo.HostName;
Console.WriteLine("FQDN: {0}", fqdn);
}
}

Note that with a misconfigured DNS, the DNS lookup may fail, or you may get the rather useless "localhost.localdomain".

If you wish to emulate your original approach, you can use the following code to retrieve the domainname:

var domainname = new StringBuilder(256);
Mono.Unix.Native.Syscall.getdomainname(domainname,
(ulong) domainname.Capacity - 1);

You will need to add the Mono.Posix assembly to your build for this.

How do I get the local machine name in C#?

System.Environment.MachineName

It works unless a machine name has more than 15 characters.



Related Topics



Leave a reply



Submit