Click here to Skip to main content
12,896,108 members (65,209 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


5 bookmarked
Posted 5 Aug 2010

How to get Domain Name (pre-Windows 2000) from FQDN (Fully Qualified Domain Name) using C#

, 5 Aug 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
How to get Domain Name (pre-Windows 2000) from FQDN (Fully Qualified Domain Name) using C#

This started when I saw a question on stack overflow and it challenged me to solve it. It may look simple initially but least to say it was challenging. The question was about getting a list of all domain names in your network and displaying it in a friendly way like what you see when you log on to Windows rather than

All you have to do is to refer to a COM Reference called Active DS Library which contains the IADsNameTranslate interface which can translate distinguished names (DNs) among various formats.

Now having that tool, it's just a matter of getting a list of domains which I can get in FQDN format easily by Getting the DomainCollection from the Forest you are in. The last issue is converting that FQDN to a DN format which is easy by using String Methods. We are doing this as the reference I mentioned above can only convert from other formats which is defined here and FQDN is not an option so the easiest would be DN. And here is how I did it.

using System.DirectoryServices.ActiveDirectory;
using ActiveDs;

private void ListDomains()
 string sUserName = "xxxx";
 string sPassword = "xxxx";

 DirectoryContext oDirectoryContext = 
	new DirectoryContext(DirectoryContextType.Domain, sUserName, sPassword);

 Domain oCurrentDomain = Domain.GetDomain(oDirectoryContext);
 Forest oForest = oCurrentDomain.Forest;
 DomainCollection oAddDomainsInForest = oForest.Domains;

 foreach (Domain oDomain in oAddDomainsInForest)
private string GetName(string sDomainName)
 IADsADSystemInfo oSysInfo = new ADSystemInfoClass();
 IADsNameTranslate oNameTranslate = new NameTranslateClass();

 string[] aSplitDN = sDomainName.Split(new Char[] { '.' });
 string sDistinguishedName = "";

 //Convert Domain Name to Distinguished Name
 foreach (string sDomainPart in aSplitDN)
 sDistinguishedName = sDistinguishedName + "DC=" + sDomainPart + ",";

    sDistinguishedName.Remove(sDistinguishedName.Length - 1));//Remove the last comma
 string sFriendlyName = oNameTranslate.Get((int)ADS_NAME_TYPE_ENUM.ADS_NAME_TYPE_NT4);
 return sFriendlyName.Replace(@"\", "");
 return "Access Denied";

If there's an easy way just let me know, but for now this works for me!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Raymund Macaalay
Technical Lead
New Zealand New Zealand

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 6 Aug 2010
Article Copyright 2010 by Raymund Macaalay
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid