Reputation: 575
I'm trying to figure out how I can get a DNS name field from Active Directory in my C# Windows Forms application. I have a computer name, and I wants to get the DNS name of this computer from Active directory. Any suggestions?
Upvotes: 0
Views: 1217
Reputation: 40928
The DNS name is stored in an attribute called dNSHostName
on the AD object for the computer, so you need to find the object in AD and read that attribute.
Here's an example, where the variable computerName
has the computer name.
var search = new DirectorySearcher {
Filter = $"(&(objectCategory=computer)(sAMAccountName={computerName}$))"
};
search.PropertiesToLoad.Add("dNSHostName");
var result = search.FindOne();
var dnsName = result.Properties["dNSHostName"][0].ToString();
I'm not setting SearchRoot
in the DirectorySearcher
here, so this will only search the current domain you're logged into.
Upvotes: 1