none
System.Directory services RRS feed

  • Question

  • I am trying to print all the users in a domain.

    My code here is

    using System;
    using System.DirectoryServices;
    using System.Collections.Generic;

    namespace listusers
    {
        class Program
        {
            static void Main(string[] args)
            {
                DirectoryEntry searchRoot = new DirectoryEntry("LDAP://DC=xxxx,DC=com");
                DirectorySearcher search = new DirectorySearcher(searchRoot);
                search.Filter = "(&(objectClass=user)(objectCategory=person))";
                search.PropertiesToLoad.Add("samAccountName");

                search.PageSize = 100;
                SearchResultCollection resultCol = search.FindAll();
                Console.WriteLine(resultCol.Count);
                int counter = 0;
                foreach(SearchResult result in resultCol)
                {               
                        Console.WriteLine(result.Properties["samAccountName"][0].ToString());
                 }
                resultCol.Dispose();
            }}}

      In the above code when I tried to use this line
    Console.WriteLine(result.Properties["samAccountName"].ToString());

    it prints the name some string.After I added [0] near the properties, the output gets printed correctly.why is that?

                     
    Wednesday, May 30, 2018 11:24 AM

Answers