Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old June 30th, 2003, 11:08 AM
Registered User
Join Date: Jun 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Active Directory Searching

As I am new to C# programming in general i am finding it very difficult to accomplish the following.

I have simple widow’s forms program with a text box, button and a list box. I need to be able to enter in a group name into the txt box and have all the users of the group be returned into the list box.

I have been going nuts trying to understand Windows Directory Services etc.

Any help would greatly be appreciated.

Old July 11th, 2003, 03:41 PM
Authorized User
Join Date: Jul 2003
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to merediths


   Do you need to use LDAP for this particular task; it may be easier to use WMI -- however this will only work on windows hosts. I don't know the name of the object you want to query off the top of my head, but if you can track it down, here's the rest ( I'm pulling this from an app I wrote to pull process info, but the theory is the same):

         1). Create a ConnectionOptions Object

         ConnectionOptions oConn = new ConnectionOptions();
            oConn.Username = txtUsername.Text;
            oConn.Password = txtPassword.Text;
   The default behavior is to construct an object with the currently logged in users credentials ... the if statement referes to controlls on the form that allow you to specify a different user (very usefull in a web context).

       System.Management.ManagementScope oMs = new System.Management.ManagementScope("\\\\" + txtComputername.Text + "\\root\\cimv2" , oConn);

System.Management.ObjectQuery oQuery = new System.Management.ObjectQuery("SELECT * FROM Win32_Process");
        ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQuery);
            ManagementObjectCollection oReturnCollection = oSearcher.Get();

            foreach(ManagementObject oReturn in oReturnCollection){
                // name of process
                txtOutput.AppendText( oReturn["Name"].ToString().ToLower() + "\n");
                // arg to send with method invoke to return user and domain
                string[] o = new String[2];
                // Invoke the method and populate the o var with the user name and domain
                // write out user info that was returned
                txtOutput.AppendText("User: " + o[1] + "\\" + o[0]);
                txtOutput.AppendText("PID: " + oReturn["ProcessId"].ToString());
                // get priority
                if(oReturn["Priority"] != null)
                    txtOutput.AppendText("Priority: " + oReturn["Priority"].ToString());
                // this is the amount of memory used
                if(oReturn["WorkingSetSize"] != null){
                    long mem = Convert.ToInt64(oReturn["WorkingSetSize"].ToString()) / 1024;
                    txtOutput.AppendText("Mem Usage: " + mem.ToString());
        }catch (System.Management.ManagementException me){

All you have to do to adapt this code too your needs is to replace the WQL "SELECT * FROM Win32_Process" with one that queries users ... i.e. "SELECT * FROM <whatever_user_object>" and then, in the for loop, you would do like an:
(pseudo code; will not compile)
  ArrayList lstUsersInGroup = new ArrayList();
   if (objUser.Group == txtGroup.text) lstUsersInGroup.Add(objUser);


Meredith Shaebanyan

Similar Threads
Thread Thread Starter Forum Replies Last Post
Searching for a user in Active Directory with ASP Ricapar Classic ASP Professional 1 July 15th, 2008 11:07 PM
Searching Active Directory mendezw@hotmail.com General .NET 0 November 30th, 2004 12:10 PM
Searching Directory Engine Renich PHP How-To 4 October 29th, 2004 09:07 AM
Searching Active Directory schenkp ASP.NET 1.0 and 1.1 Basics 4 September 21st, 2004 11:35 AM
Searching Active Directory levinll VB How-To 1 June 8th, 2004 08:47 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.