简体   繁体   English

通过计算机名称C#AD获取DNS名称

[英]Get DNS name by computer name C# AD

I'm trying to figure out how I can get a DNS name field from Active Directory in my C# Windows Forms application. 我试图弄清楚如何在C#Windows Forms应用程序中从Active Directory获取DNS名称字段。 I have a computer name, and I wants to get the DNS name of this computer from Active directory. 我有一个计算机名,并且我想从Active Directory中获得这台计算机的DNS名称。 Any suggestions? 有什么建议么?

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. DNS名称存储在计算机的AD对象上名为dNSHostName的属性中,因此您需要在AD中找到该对象并读取该属性。

Here's an example, where the variable computerName has the computer name. 这是一个示例,其中变量computerName具有计算机名称。

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. 我不在这里的DirectorySearcher设置SearchRoot ,因此这只会搜索您登录的当前域。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM