[ACCEPTED]-How do I find the fully qualified hostname of my machine in C#?-environment-variables

Accepted answer
Score: 52

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

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

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

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

Score: 13

I used this very similar method:

var serverName = System.Environment.MachineName; //host name sans domain
var fqhn = System.Net.Dns.GetHostEntry(serverName).HostName; //fully qualified hostname

0

Score: 2

If the above doesn't work, you can also 1 try retrieving it from the environment:

var dnsName = new StringBuilder();
dnsName.Append(Environment.GetEnvironmentVariable("COMPUTERNAME")).Append(".");
dnsName.Append(Environment.GetEnvironmentVariable("USERDNSDOMAIN"));

More Related questions