2014-10-16 124 views
1

我試圖從Active Directory中獲取所有用戶。DirectorySearcher.FindAll卡住

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

問題是我沒有啓用(調試 - >例外)中的'公共語言運行時異常'。 DS.FindAll()除外;有運行時,所以它停止執行剩餘的代碼。