Tuesday, November 23, 2010

How to get the Domain Name by Controller Name or Server Name using C#

Today I ran into this problem and after few hours of googling i came up with this. Hope this will help someone as well.

static void Main() {

string domainservername = "xxx";
string domainusername = "xxx";
string domainpassword = "xxx";

Add reference to System.DirectoryServices.dll

DirectoryContext context = new DirectoryContext(DirectoryContextType.DirectoryServer, domainservername, domainusername, domainpassword);

DomainController controller = DomainController.FindOne(context);
string domainName = controller.Domain.Name;
}

No comments: