CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 1999
    Posts
    170

    How to programatically find out (enumerate) all existing Windows User-Groups ??

    Hi to all

    What I need is to enumerate all existing windows user-groups (those like Administrators, Power-Users etc. but even my own created groups, I think you know what I mean) - all within some C# code.

    I thought this might be easy but I searched all the Internet to find the solution, however, I still can't do it...

    Is there anybody who could help me ???
    Thanks a lot, I really really need your help.

    Honza

  2. #2
    Join Date
    Dec 2000
    Location
    Slovakia
    Posts
    1,043
    If you are talking about the AD groups (or domain groups):
    Code:
    using System;
    using System.DirectoryServices;
    
    class Class1
    {
        [STAThread]
        static void Main(string[] args)
        {
            DirectoryEntry entry = new DirectoryEntry("LDAP://YourDomainName");
            DirectorySearcher searcher = new DirectorySearcher(entry);
    
            searcher.Filter = "(objectClass=group)";
    
            foreach (SearchResult res in searcher.FindAll())
            {
                Console.WriteLin(res.GetDirectoryEntry().Name.ToString());
            }
        }
    }
    Martin

  3. #3
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    excellent martin, I should bookmark it.

    Paresh

  4. #4
    Join Date
    Feb 2003
    Location
    Israel
    Posts
    102
    I agree with pareshgh

  5. #5
    Join Date
    May 2003
    Location
    upstate NY
    Posts
    168
    Originally posted by MartinL
    If you are talking about the AD groups (or domain groups):
    Code:
    using System;
    using System.DirectoryServices;
    
    class Class1
    {
        [STAThread]
        static void Main(string[] args)
        {
            DirectoryEntry entry = new DirectoryEntry("LDAP://YourDomainName");
            DirectorySearcher searcher = new DirectorySearcher(entry);
    
            searcher.Filter = "(objectClass=group)";
    
            foreach (SearchResult res in searcher.FindAll())
            {
                Console.WriteLin(res.GetDirectoryEntry().Name.ToString());
            }
        }
    }
    Martin
    Very cool ..
    I do have a question that kind of goes with this.
    Is there a way to findout what users belong to each group?

    So I could generate a listing
    GROUP:
    GroupName
    USERS IN GROUP:
    User1
    User2
    ...... ect

    Thanks
    Will
    --------------------------------------------
    Tell me and I will forget
    Show me and I will remember
    Teach me and I will learn

  6. #6
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125
    You can also use WindowsPrincipals.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured