共用方式為


列舉大型群組的成員

此主題說明範圍擷取的運作方式,並提供數個程式碼範例說明如何使用範圍擷取來取得群組的成員。adschema 群組物件包含名為 member 的屬性,此屬性包含陣列中的數個值。如需有關群組物件或 adschema 中之 member 屬性的詳細資訊,請參閱 MSDN Library (https://go.microsoft.com/fwlink/?LinkID=27252 (本頁面可能為英文)) 中的<Group>或<Member>主題。

因為群組成員資格有時會非常大,此屬性可能包含上百個值。範圍擷取是一次取得一部分成員的程序。對於 Windows Server 2003,一次可從伺服器擷取的值數目上限是 1500 個。若將範圍取設定為大於集合中的值數目,則搜尋會失敗。若將範圍設定為較小的數字,則搜尋的效能會變差,因為必須更頻繁地返回伺服器以取得新結果。如需範圍擷取的詳細資訊,請參閱<列舉包含許多成員的群組>。

下列程式碼範例示範如何使用範圍擷取來取得群組的成員。此範例擷取第 0-500 個項目,包含第 0 個與第 500 個項目。此結果集的最大項目是 501。

DirectoryEntry group = new DirectoryEntry("LDAP://CN=Sales,DC=Fabrikam,DC=COM");
DirectorySearcher groupMember = new DirectorySearcher
    (group,"(objectClass=*)",new string[]{"member;Range=0-500"},SearchScope.Base);
SearchResult result = groupMember.FindOne();
// Each entry contains a property name and the path (ADsPath).
// The following code returns the property name from the PropertyCollection. 
String propName=String.Empty;
foreach(string s in result.Properties.PropertyNames)
{
    if ( s.ToLower() != "adspath")
    {
      propName = s;
      break;
    }
}
foreach(string member in result.Properties[propName])
{
     Console.WriteLine(member);
}

您也可以透過在結果集的特定點開始與結束,以使用範圍擷取來擷取結果集的一部分。若要這樣做,請修改 {"member;Range=0-500"} 陳述式。例如,若要擷取結果集中的第三個與第四個項目,您可以使用陳述式 {"member;Range=2-3"}。若要擷取所有項目 (從 502 開始到結果集的結尾),請使用陳述式 {"member;Range=501-*"}。

最後的程式碼範例示範如何在不知道群組成員數目的情況下,使用範圍擷取來取得群組的所有成員。因為當您嘗試取得超過群組成員數目時範圍擷取無法運作,此程式碼範例會測試失敗情況,並在擷取時將範圍陳述式變更為 ("member;range={0}-*", rangeLow) 以列舉集合中的最後一個成員。

try
{
    DirectoryEntry entry = new DirectoryEntry("LDAP://CN=My Distribution List,OU=Distribution Lists,DC=Fabrikam,DC=com");
    DirectorySearcher searcher = new DirectorySearcher(entry);
    searcher.Filter = "(objectClass=*)";

    uint rangeStep = 1000;
    uint rangeLow = 0;
    uint rangeHigh = rangeLow + (rangeStep - 1);
    bool lastQuery = false;
    bool quitLoop = false;

    do
    {
        string attributeWithRange;
        if(!lastQuery)
        {
            attributeWithRange = String.Format("member;range={0}-{1}", rangeLow, rangeHigh);
        }
        else
        {
            attributeWithRange = String.Format("member;range={0}-*", rangeLow);
        }           
        searcher.PropertiesToLoad.Clear();
        searcher.PropertiesToLoad.Add(attributeWithRange);
        SearchResult results = searcher.FindOne();
        searcher.Dispose();
        foreach(string res in results.Properties.PropertyNames)
        {
            System.Diagnostics.Debug.WriteLine(res.ToString());
        }
        if(results.Properties.Contains(attributeWithRange))
        {
            foreach(object obj in results.Properties[attributeWithRange])
            {
                Console.WriteLine(obj.GetType());
                if(obj.GetType().Equals(typeof(System.String)))
                {
                }
                else if (obj.GetType().Equals(typeof(System.Int32)))
                {
                }
                Console.WriteLine(obj.ToString());
            }
            if(lastQuery)
            {
                quitLoop = true;
            }
        }
        else
        {
            lastQuery = true;
        }
        if(!lastQuery)
        {
            rangeLow = rangeHigh + 1;
            rangeHigh = rangeLow + (rangeStep - 1);
        }
    }
    while(!quitLoop);
}
catch(Exception ex)
{
    // Handle exception ex.
}

請參閱

參考

System.DirectoryServices

概念

群組管理

Send comments about this topic to Microsoft.

Copyright © 2007 by Microsoft Corporation.All rights reserved.