如何获取所有域的列表? [英] How to get a list of all domains?

查看:230
本文介绍了如何获取所有域的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让在Windows登录对话框中是可用的(在Domain下拉列表)中的所有领域。



我试过下面的代码,但它只返回我登录到域。 ?我失去了一些东西。



  StringCollection的domainlist =新StringCollection(); 

{
的DirectoryEntry EN =新的DirectoryEntry();
//搜索objectCategory属性类型域
的DirectorySearcher SRCH =新的DirectorySearcher(恩,objectCategory属性=域);
SearchResultCollection科尔= srch.FindAll();
//枚举每个返回域。
的foreach(在科尔信息搜索结果RS)
{
ResultPropertyCollection resultPropColl = rs.Properties;
的foreach(在resultPropColl对象则domainName [名称])
{
domainList.Add(domainName.ToString());
}
}
}
赶上(异常前)
{
Trace.Write(ex.Message);
}
返回的domainlist;


解决方案

添加到System.DirectoryServices.dll程序<参考/ p>

 使用(VAR森林= Forest.GetCurrentForest())
{
的foreach(在forest.Domains域的域)
{
的Debug.WriteLine(domain.Name);
domain.Dispose();
}
}


I'm trying to get all domains that are available in the Windows Login dialog (in the Domain dropdown).

I've tried the following code but it only returns the domain I am logged into. Am I missing something?

StringCollection domainList = new StringCollection();
try
{
    DirectoryEntry en = new DirectoryEntry();
    // Search for objectCategory type "Domain"
    DirectorySearcher srch = new DirectorySearcher(en, "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;

解决方案

Add a reference to System.DirectoryServices.dll

using (var forest = Forest.GetCurrentForest())
{
    foreach (Domain domain in forest.Domains)
    {
        Debug.WriteLine(domain.Name);
        domain.Dispose();
    }
}

这篇关于如何获取所有域的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆