我正在尝试从Active Directory中获取所有用户。
private void Form1_Load(object sender, EventArgs e)
{
string[] RetProps = new string[] { "SamAccountName", "DisplayName" };
List<string[]> users = new List<string[]>();
foreach (SearchResult User in GetAllUsers("localhost", RetProps))
{
DirectoryEntry DE = User.GetDirectoryEntry();
try
{
users.Add(new string[] { DE.Properties["SamAccountName"][0].ToString(), DE.Properties["DisplayName"][0].ToString() });
}
catch
{
}
}
}
internal static SearchResultCollection GetAllUsers(string DomainName, string[] Properties)
{
DirectoryEntry DE = new DirectoryEntry("LDAP://" + DomainName);
string Filter = "(&(objectCategory=organizationalPerson)(objectClass=User))";
DirectorySearcher DS = new DirectorySearcher(DE);
DS.PageSize = 10000;
DS.SizeLimit = 10000;
DS.SearchScope = SearchScope.Subtree;
DS.PropertiesToLoad.AddRange(Properties); DS.Filter = Filter;
SearchResultCollection RetObjects = DS.FindAll();
return RetObjects;
}
但是在到达DS.FindAll();
GetAllUsers
函数时,它会被卡住。
答案 0 :(得分:1)
问题在于我没有启用“公共语言运行时”例外&#39; in(Debug-&gt; Exceptions)。除了DS.FindAll();有运行时,所以它停止执行剩余的代码。