要以编程方式找出Windows中的域控制器,可以使用以下方法:
示例代码(C#):
using System;
using System.DirectoryServices;
public class DomainControllerFinder
{
public static void Main()
{
string domainName = "example.com";
string domainController = FindDomainController(domainName);
Console.WriteLine("Domain controller for {0} is {1}", domainName, domainController);
}
public static string FindDomainController(string domainName)
{
string ldapPath = string.Format("LDAP://{0}", domainName);
using (DirectoryEntry de = new DirectoryEntry(ldapPath))
{
string domainController = de.Properties["dc"].Value.ToString();
return domainController;
}
}
}
示例代码(C#):
using System;
using System.Net;
public class DomainControllerFinder
{
public static void Main()
{
string domainName = "example.com";
string domainController = FindDomainController(domainName);
Console.WriteLine("Domain controller for {0} is {1}", domainName, domainController);
}
public static string FindDomainController(string domainName)
{
string domainController = string.Format("_ldap._tcp.dc._msdcs.{0}", domainName);
IPHostEntry hostEntry = Dns.GetHostEntry(domainController);
return hostEntry.HostName;
}
}
这两种方法都可以用来查询域控制器,可以根据实际需求选择合适的方法。
领取专属 10元无门槛券
手把手带您无忧上云