Aravind Sivam
Aravind Sivam

Reputation: 1099

DirectorySearcher.FindAll gets stuck

I'm trying to get all the user from 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;
 }

But on reaching the DS.FindAll(); in GetAllUsers function, it gets stuck.

Upvotes: 1

Views: 566

Answers (1)

Aravind Sivam
Aravind Sivam

Reputation: 1099

The issues is that i didn't enables the 'Common Language Runtime exceptions' in (Debug->Exceptions). On exception of DS.FindAll(); there is the runtime so it's stop executing the remaining code.

Upvotes: 1

Related Questions