Thursday, March 31, 2011

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

Ex : I want something like abc.hyd.mycompany.com. My requirement is to parse this name and initialize appropriate service.

use System.net;
Dns.GetHostName() doesn't return fully qualified name it just gives "abc"
From stackoverflow
  • You may be able to get the whole DNS string like this:

    System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).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.

    Rick Glos : That worked on my machine :)
    : This works. Thanks a lot :)
  • If the above doesn't work, you can also try retrieving it from the environment:

    var dnsName = new StringBuilder();
    dnsName.Append(Environment.GetEnvironmentVariable("COMPUTERNAME")).Append(".");
    dnsName.Append(Environment.GetEnvironmentVariable("USERDNSDOMAIN"));
    
    pduncan : This may not work - USERDNSDOMAIN may not be the same as the domain that the machine belongs to.

0 comments:

Post a Comment