我需要获取网络上的域名列表......
但我只获得了我登录的域名... 所以例如有2个域“xyz”和“xyz2” 但我只获得了登录的域名....
这是我的代码:
if (!IsPostBack)
{
StringCollection adDomains = this.GetDomainList();
foreach (string strDomain in adDomains)
{
DropDownList1.Items.Add(strDomain);
}
}
}
private StringCollection GetDomainList()
{
StringCollection domainList = new StringCollection();
try
{
DirectoryEntry en = new DirectoryEntry("LDAP://");
// Search for objectCategory type "Domain"
DirectorySearcher srch = new DirectorySearcher("objectCategory=Domain");
SearchResultCollection coll = srch.FindAll();
// Enumerate over each returned domain.
foreach (SearchResult rs in coll)
{
ResultPropertyCollection resultPropColl = rs.Properties;
foreach (object domainName in resultPropColl["name"])
{
domainList.Add(domainName.ToString());
}
}
}
catch (Exception ex)
{
Trace.Write(ex.Message);
}
return domainList;
}
答案 0 :(得分:2)
using System.DirectoryServices.ActiveDirectory;
....
Forest currentForest = Forest.GetCurrentForest();
DomainCollection domains = currentForest.Domains;
foreach(Domain objDomain in domains)
{
System.Diagnostics.Debug.WriteLine(objDomain.Name);
}