Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8647601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:06:35+00:00 2026-06-12T13:06:35+00:00

I know there are lots of methods already given in stackoverflow but in my

  • 0

I know there are lots of methods already given in stackoverflow but in my case all of them taking too long time. I post a method which takes less time but still it is too long to implement. Please help me so that it takes less execution time. Also take consideration that i am using .net 2.0 framework.

        try
        {
            List<string> lstEmails = new List<string>();
            string filter1 = string.Format("(anr={0})", "groupname");
            DirectorySearcher searcher = new DirectorySearcher(entry);
            searcher.Filter = filter1;
            searcher.SearchScope = SearchScope.Subtree;
            searcher.PropertiesToLoad.Add("mail");
            IEnumerable res = (IEnumerable)searcher.FindOne().GetDirectoryEntry().Invoke("members");
            //IEnumerable<string> rest = (IEnumerable<string>)res;

            if (res != null)
            {
                try
                {
                    int index = 0;
                    foreach (IEnumerable resl in res)
                    {
                        DateTime start = DateTime.Now;
                        DirectoryEntry dr = new DirectoryEntry(resl);
                        string strEmail = null;
                        if (dr.Properties["mail"].Value != null)
                        {
                            strEmail = dr.Properties["mail"].Value.ToString();
                            Console.WriteLine(strEmail);
                            DateTime stop = DateTime.Now;
                            Console.WriteLine((stop - start).TotalMinutes.ToString());
                            index++;
                            Console.WriteLine(index.ToString());
                        }
                        if (!string.IsNullOrEmpty(strEmail))
                        {
                            // groupMemebers.Add("sam",strEmail);
                        }
                    }
                }
                catch { }
            }


        }
        catch { }

This is your suggested method Daro..

    DirectoryContext context = new DirectoryContext(DirectoryContextType.Domain, domain, "domainname" + strLDAPUserName, strLDAPPassword);
        DomainController controller = DomainController.FindOne(context);
        DirectoryEntry entry = new DirectoryEntry(string.Format("LDAP://{0}",controller.Domain), strLDAPUserName, strLDAPPassword, AuthenticationTypes.Secure);
List<string> userList = new List<string>();  
        DateTime StartTime = DateTime.Now;   
        using (DirectorySearcher ds = new DirectorySearcher(entry)) 
        {
            ds.PropertiesToLoad.Add("mail");  
            ds.PageSize = 10000;
            string DistingushiedName = "CN=" + groupName + ",OU=Users,dc=CompanyName,dc=com";
            ds.Filter = "(&(objectClass=user)(memberof:1.2.840.113556.1.4.1941:="+DistingushiedName+"))";   
            ds.SearchScope = SearchScope.Subtree; 
            try 
            {
                foreach (SearchResult user in ds.FindAll())   
                {
                    try  
                    {
                        userList.Add(user.Path);//.Properties["mail"][0].ToString()); 
                    }
                    catch (Exception E)    
                    {
                        throw new Exception(E.Message);
                    }
                }
            }
            catch(Exception E)    
            {
                throw new Exception(E.Message); 
            }
            DateTime EndTime = DateTime.Now;
            TimeSpan Dif = EndTime.Subtract(StartTime);
        } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T13:06:36+00:00Added an answer on June 12, 2026 at 1:06 pm

    Hey this is the correct way…

      try
            {
                List<string> ReturnArray = new List<string>();
                DirectoryContext context = new DirectoryContext(DirectoryContextType.Domain, domainName, domainName + "\\" + UserName, Password);
                DomainController controller = DomainController.FindOne(context);
                string LDAPAddress = string.Format("LDAP://{0}", controller.Domain);
                DirectoryEntry deDirEntry = new DirectoryEntry(LDAPAddress, UserName, Password);
                deDirEntry.AuthenticationType = AuthenticationTypes.Secure;
    
                DirectorySearcher mySearcher = new DirectorySearcher(deDirEntry);
                mySearcher.PropertiesToLoad.Add("distinguishedName");
                string sFilter = String.Format("(&(objectcategory=group)(cn=" + GroupName + "))");
    
                mySearcher.Filter = sFilter;
                mySearcher.Sort.Direction = SortDirection.Ascending;
                mySearcher.Sort.PropertyName = "cn";
                SearchResult result;
                DirectoryEntry ResultEntry;
                result = mySearcher.FindOne();
                ResultEntry = result.GetDirectoryEntry();
                GroupName = ResultEntry.Properties["distinguishedName"].Value.ToString();
                mySearcher = new DirectorySearcher(deDirEntry);
                mySearcher.PropertiesToLoad.Add("cn");
                mySearcher.PropertiesToLoad.Add("mail");
                sFilter = String.Format("(&(objectClass=person)(memberOf={0}))", GroupName);
                mySearcher.Filter = sFilter;
                mySearcher.Sort.Direction = SortDirection.Ascending;
                mySearcher.Sort.PropertyName = "cn";
                SearchResultCollection results;
                results = mySearcher.FindAll();
                foreach (SearchResult resEnt in results)
                {
                    ResultPropertyCollection propcoll = resEnt.Properties;
                    foreach (string key in propcoll.PropertyNames)
                    {
                        if (key == "mail")
                        {
                            foreach (object values in propcoll[key])
                            {
                                if (!String.IsNullOrEmpty(values.ToString()))
                                {
                                    ReturnArray.Add(values.ToString());
                                    Console.WriteLine(values.ToString());
                                }
                            }
                        }
                    }
                }
    
                return ReturnArray;
            }
            catch
            {
                return null;
            }
    

    Thanks everyone for your valuable suggesstion

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that there are lots of examples out there on this, but they
I know that there are lot's of questons on this, but all seem to
I know there are lots of delegate/func examples but I can't find any examples
I know there are lots of questions to do with this already, and I've
I know there are lots of tools on the net that can make our
I know there are LOTS of reasons why you would compose a certain object
1) I know there are lots of web sites that describe in what order
I know that there is an allocator for user applications than handles lots of
I know there is a lot on this topic but I can't get any
I've been developing in Objective-C for a long time, mostly games, and lots of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.