获取分页DirectorySearcher的搜索结果总数

时间:2015-08-27 11:00:05

标签: c# active-directory

我使用DirectorySearch搜索Active Directory中的对象。我设置VirtualListView属性只返回几个值:

using System;
using System.DirectoryServices;

namespace Testbed
{
    internal class Testbed
    {
        private static void Main()
        {
            DoSearch();
            Console.ReadKey();
        }

        private static void DoSearch()
        {
            var entry = new DirectoryEntry("LDAP://server/DC=mydomain,DC=com", @"USERNAME", "PASSWORD");
            var searcher = new DirectorySearcher(entry)
            {
                PageSize = 0,
                SearchScope = SearchScope.Subtree,
                Filter = "(Description=J_*)",
                Sort = new SortOption("Description", SortDirection.Ascending),
                VirtualListView = new DirectoryVirtualListView(0, 9, 1)
            };

            SearchResultCollection results = searcher.FindAll();
            foreach (SearchResult result in results)
            {
                Console.WriteLine(result.Properties["Description"][0]);
            }
            Console.WriteLine("Found: " + results.Count);
        }
    }
}

有没有办法在不进行明确的DirectorySearch - “调用”和PageSize(例如500)的情况下,为查询获取AD中的对象总数?

1 个答案:

答案 0 :(得分:1)

<html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script> <script src="jquery.js"></script> </head> <body> <form action='' method='POST' id='size_form'> <p>search for size</p> <input type='text' name="batch_size" id='batchsize'/> <input type='submit' value='Submit'id='size_button' method='POST'/> </form> <div id='response'></div> </body> </html> 类中有ApproximateTotal个属性。您可以使用它,但请记住您应该在代码中的DirectoryVirtualListView块之后访问它。 只需更改此行:

foreach

到这一个:

Console.WriteLine("Found: " + results.Count);

全部完成!