using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.DirectoryServices;
using System.IO;
namespace ActiveDirQuery
{
/// <summary>
/// Summary description for ADQForm.
/// </summary>
public class ADQForm : Form
{
private TreeView ObjectView; // the tree view on the main form
private GroupBox groupBox1;
private CheckBox UsersCB, ComputersCB, PrintersCB;
private Label FilterLabel1, FilterLabel2, FilterLabel3;
private ImageList myImageList; // image list for the tree view
// query filters for fetching the objects
private TextBox UsersFilter, ComputersFilter, PrintersFilter;
// state of the query
public bool Querying = false;
// basic nodes for the tree view
private TreeNode RootNode; // this node should show the domain server name
private TreeNode UsersNode, ComputersNode, PrintersNode;
private Button QueryButton;
private Button ExitButton;
private GroupBox groupBox2;
private Label label1;
private Label label2;
private Label label3;
private Label label4;
private GroupBox groupBox3;
private Label label5;
private Label label6;
private Label label7;
private TextBox PageTimeLimit;
private TextBox TotalTimeLimit;
private TextBox ObjsPerPage;
private TextBox ObjsToFetch;
private ComboBox searchOptionCB;
private CheckBox SortResultsCB;
private CheckBox CacheResultsCB;
private Label label8;
/// <summary>
/// Required designer variable.
/// </summary>
private Container components = null;
public ADQForm()
{
InitializeComponent();
searchOptionCB.SelectedIndex = 0;
// load images from the icon files
myImageList = new ImageList();
myImageList.Images.Add(new Icon("button.ico"));
myImageList.Images.Add(new Icon("domain.ico"));
myImageList.Images.Add(new Icon("computer.ico"));
myImageList.Images.Add(new Icon("users.ico"));
myImageList.Images.Add(new Icon("printer.ico"));
// assign the imagelist to the tree view
ObjectView.ImageList = myImageList;
// prepare the root node
GetRootNode();
ComputersNode = new TreeNode("Computers", 2, 2);
UsersNode = new TreeNode("Users", 3, 3);
PrintersNode = new TreeNode("Printers", 4, 4);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(ADQForm));
this.ObjectView = new TreeView();
this.groupBox1 = new GroupBox();
this.FilterLabel3 = new Label();
this.FilterLabel2 = new Label();
this.FilterLabel1 = new Label();
this.PrintersCB = new CheckBox();
this.ComputersCB = new CheckBox();
this.UsersCB = new CheckBox();
this.UsersFilter = new TextBox();
this.ComputersFilter = new TextBox();
this.PrintersFilter = new TextBox();
this.ExitButton = new Button();
this.QueryButton = new Button();
this.groupBox2 = new GroupBox();
this.label3 = new Label();
this.PageTimeLimit = new TextBox();
this.label2 = new Label();
this.ObjsPerPage = new TextBox();
this.label1 = new Label();
this.searchOptionCB = new ComboBox();
this.label4 = new Label();
this.groupBox3 = new GroupBox();
this.TotalTimeLimit = new TextBox();
this.label6 = new Label();
this.label5 = new Label();
this.ObjsToFetch = new TextBox();
this.label7 = new Label();
this.SortResultsCB = new CheckBox();
this.CacheResultsCB = new CheckBox();
this.label8 = new Label();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.groupBox3.SuspendLayout();
this.SuspendLayout();
//
// ObjectView
//
this.ObjectView.Dock = DockStyle.Bottom;
this.ObjectView.ImageIndex = -1;
this.ObjectView.Location = new System.Drawing.Point(0, 264);
this.ObjectView.Name = "ObjectView";
this.ObjectView.SelectedImageIndex = -1;
this.ObjectView.Size = new System.Drawing.Size(458, 261);
this.ObjectView.TabIndex = 0;
//
// groupBox1
//
this.groupBox1.Controls.Add(this.FilterLabel3);
this.groupBox1.Controls.Add(this.FilterLabel2);
this.groupBox1.Controls.Add(this.FilterLabel1);
this.groupBox1.Controls.Add(this.PrintersCB);
this.groupBox1.Controls.Add(this.ComputersCB);
this.groupBox1.Controls.Add(this.UsersCB);
this.groupBox1.Controls.Add(this.UsersFilter);
this.groupBox1.Controls.Add(this.ComputersFilter);
this.groupBox1.Controls.Add(this.PrintersFilter);
this.groupBox1.Location = new System.Drawing.Point(10, 8);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(330, 102);
this.groupBox1.TabIndex = 1;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Objects to query";
//
// FilterLabel3
//
this.FilterLabel3.Enabled = false;
this.FilterLabel3.Location = new System.Drawing.Point(86, 80);
this.FilterLabel3.Name = "FilterLabel3";
this.FilterLabel3.Size = new System.Drawing.Size(70, 18);
this.FilterLabel3.TabIndex = 5;
this.FilterLabel3.Text = "Name Filter :";
//
// FilterLabel2
//
this.FilterLabel2.Enabled = false;
this.FilterLabel2.Location = new System.Drawing.Point(86, 50);
this.FilterLabel2.Name = "FilterLabel2";
this.FilterLabel2.Size = new System.Drawing.Size(70, 18);
this.FilterLabel2.TabIndex = 4;
this.FilterLabel2.Text = "Name Filter :";
//
// FilterLabel1
//
this.FilterLabel1.Enabled = false;
this.FilterLabel1.Location = new System.Drawing.Point(86, 22);
this.FilterLabel1.Name = "FilterLabel1";
this.FilterLabel1.Size = new System.Drawing.Size(70, 18);
this.FilterLabel1.TabIndex = 3;
this.FilterLabel1.Text = "Name Filter :";
//
// PrintersCB
//
this.PrintersCB.Location = new System.Drawing.Point(8, 76);
this.PrintersCB.Name = "PrintersCB";
this.PrintersCB.Size = new System.Drawing.Size(80, 24);
this.PrintersCB.TabIndex = 2;
this.PrintersCB.Text = "Printers";
this.PrintersCB.CheckedChanged += new EventHandler(this.OnPrintersCheckedChanged);
//
// ComputersCB
//
this.ComputersCB.Location = new System.Drawing.Point(8, 48);
this.ComputersCB.Name = "ComputersCB";
this.ComputersCB.Size = new System.Drawing.Size(80, 24);
this.ComputersCB.TabIndex = 1;
this.ComputersCB.Text = "Computers";
this.ComputersCB.CheckedChanged += new EventHandler(this.OnComputersCheckedChanged);
//
// UsersCB
//
this.UsersCB.Location = new System.Drawing.Point(8, 20);
this.UsersCB.Name = "UsersCB";
this.UsersCB.Size = new System.Drawing.Size(80, 24);
this.UsersCB.TabIndex = 0;
this.UsersCB.Text = "Users";
this.UsersCB.CheckedChanged += new EventHandler(this.OnUsersCheckedChanged);
//
// UsersFilter
//
this.UsersFilter.Enabled = false;
this.UsersFilter.Location = new System.Drawing.Point(160, 22);
this.UsersFilter.Name = "UsersFilter";
this.UsersFilter.Size = new System.Drawing.Size(162, 20);
this.UsersFilter.TabIndex = 6;
this.UsersFilter.Text = "*";
//
// ComputersFilter
//
this.ComputersFilter.Enabled = false;
this.ComputersFilter.Location = new System.Drawing.Point(160, 48);
this.ComputersFilter.Name = "ComputersFilter";
this.ComputersFilter.Size = new System.Drawing.Size(162, 20);
this.ComputersFilter.TabIndex = 7;
this.ComputersFilter.Text = "*";
//
// PrintersFilter
//
this.PrintersFilter.Enabled = false;
this.PrintersFilter.Location = new System.Drawing.Point(160, 78);
this.PrintersFilter.Name = "PrintersFilter";
this.PrintersFilter.Size = new System.Drawing.Size(162, 20);
this.PrintersFilter.TabIndex = 8;
this.PrintersFilter.Text = "*";
//
// ExitButton
//
this.ExitButton.BackColor = Color.NavajoWhite;
this.ExitButton.Location = new System.Drawing.Point(358, 76);
this.ExitButton.Name = "ExitButton";
this.ExitButton.Size = new System.Drawing.Size(88, 23);
this.ExitButton.TabIndex = 10;
this.ExitButton.Text = "Exit";
this.ExitButton.Click += new System.EventHandler(this.OnExit);
//
// QueryButton
//
this.QueryButton.BackColor = Color.LightGreen;
this.QueryButton.Image = ((System.Drawing.Image)(resources.GetObject("QueryButton.Image")));
this.QueryButton.ImageAlign = System.Drawing.ContentAlignment.TopCenter;
this.QueryButton.Location = new System.Drawing.Point(356, 14);
this.QueryButton.Name = "QueryButton";
this.QueryButton.Size = new System.Drawing.Size(88, 42);
this.QueryButton.TabIndex = 9;
this.QueryButton.Text = "Query";
this.QueryButton.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
this.QueryButton.Click += new System.EventHandler(this.OnQuery);
//
// groupBox2
//
this.groupBox2.Controls.Add(this.label3);
this.groupBox2.Controls.Add(this.PageTimeLimit);
this.groupBox2.Controls.Add(this.label2);
this.groupBox2.Controls.Add(this.ObjsPerPage);
this.groupBox2.Controls.Add(this.label1);
this.groupBox2.Location = new System.Drawing.Point(10, 120);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(212, 82);
this.groupBox2.TabIndex = 11;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "Page level options";
//
// label3
//
this.label3.Location = new System.Drawing.Point(176, 54);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(28, 14);
this.label3.TabIndex = 4;
this.label3.Text = "secs";
//
// PageTimeLimit
//
this.PageTimeLimit.Location = new System.Drawing.Point(124, 50);
this.PageTimeLimit.Name = "PageTimeLimit";
this.PageTimeLimit.Size = new System.Drawing.Size(48, 20);
this.PageTimeLimit.TabIndex = 3;
this.PageTimeLimit.Text = "";
this.PageTimeLimit.Validating += new CancelEventHandler(this.OnValidatingText);
//
// label2
//
this.label2.Location = new System.Drawing.Point(6, 52);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(120, 16);
this.label2.TabIndex = 2;
this.label2.Text = "Per page time limit :";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// ObjsPerPage
//
this.ObjsPerPage.Location = new System.Drawing.Point(124, 24);
this.ObjsPerPage.Name = "ObjsPerPage";
this.ObjsPerPage.Size = new System.Drawing.Size(48, 20);
this.ObjsPerPage.TabIndex = 1;
this.ObjsPerPage.Text = "";
this.ObjsPerPage.Validating += new CancelEventHandler(this.OnValidatingText);
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 26);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(118, 16);
this.label1.TabIndex = 0;
this.label1.Text = "Max objects per page :";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// searchOptionCB
//
this.searchOptionCB.DropDownStyle = ComboBoxStyle.DropDownList;
this.searchOptionCB.Items.AddRange(new object[] {
"Search the complete sub tree",
"Search only immediate children"});
this.searchOptionCB.Location = new System.Drawing.Point(92, 210);
this.searchOptionCB.Name = "searchOptionCB";
this.searchOptionCB.Size = new System.Drawing.Size(176, 21);
this.searchOptionCB.TabIndex = 12;
//
// label4
//
this.label4.Location = new System.Drawing.Point(12, 214);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(80, 14);
this.label4.TabIndex = 13;
this.label4.Text = "Search option :";
//
// groupBox3
//
this.groupBox3.Controls.Add(this.TotalTimeLimit);
this.groupBox3.Controls.Add(this.label6);
this.groupBox3.Controls.Add(this.label5);
this.groupBox3.Controls.Add(this.ObjsToFetch);
this.groupBox3.Controls.Add(this.label7);
this.groupBox3.Location = new System.Drawing.Point(226, 120);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(222, 84);
this.groupBox3.TabIndex = 14;
this.groupBox3.TabStop = false;
this.groupBox3.Text = "Server level options";
//
// TotalTimeLimit
//
this.TotalTimeLimit.Location = new System.Drawing.Point(128, 50);
this.TotalTimeLimit.Name = "TotalTimeLimit";
this.TotalTimeLimit.Size = new System.Drawing.Size(48, 20);
this.TotalTimeLimit.TabIndex = 7;
this.TotalTimeLimit.Text = "";
this.TotalTimeLimit.Validating += new CancelEventHandler(this.OnValidatingText);
//
// label6
//
this.label6.Location = new System.Drawing.Point(14, 52);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(112, 16);
this.label6.TabIndex = 6;
this.label6.Text = "Total time limit :";
this.label6.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// label5
//
this.label5.Location = new System.Drawing.Point(14, 26);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(112, 16);
this.label5.TabIndex = 0;
this.label5.Text = "Max objects to fetch :";
this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// ObjsToFetch
//
this.ObjsToFetch.Location = new System.Drawing.Point(128, 24);
this.ObjsToFetch.Name = "ObjsToFetch";
this.ObjsToFetch.Size = new System.Drawing.Size(48, 20);
this.ObjsToFetch.TabIndex = 5;
this.ObjsToFetch.Text = "";
this.ObjsToFetch.Validating += new CancelEventHandler(this.OnValidatingText);
//
// label7
//
this.label7.Location = new System.Drawing.Point(180, 52);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(28, 16);
this.label7.TabIndex = 5;
this.label7.Text = "secs";
this.label7.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// SortResultsCB
//
this.SortResultsCB.Location = new System.Drawing.Point(296, 214);
this.SortResultsCB.Name = "SortResultsCB";
this.SortResultsCB.Size = new System.Drawing.Size(86, 14);
this.SortResultsCB.TabIndex = 15;
this.SortResultsCB.Text = "Sort Results";
//
// CacheResultsCB
//
this.CacheResultsCB.Checked = true;
this.CacheResultsCB.CheckState = CheckState.Checked;
this.CacheResultsCB.Location = new System.Drawing.Point(296, 238);
this.CacheResultsCB.Name = "CacheResultsCB";
this.CacheResultsCB.Size = new System.Drawing.Size(98, 14);
this.CacheResultsCB.TabIndex = 16;
this.CacheResultsCB.Text = "Cache Results";
//
// label8
//
this.label8.Location = new System.Drawing.Point(10, 246);
this.label8.Name = "label8";
this.label8.Size = new System.Drawing.Size(100, 14);
this.label8.TabIndex = 17;
this.label8.Text = "Results :";
//
// ADQForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(458, 525);
this.Controls.Add(this.label8);
this.Controls.Add(this.CacheResultsCB);
this.Controls.Add(this.SortResultsCB);
this.Controls.Add(this.groupBox3);
this.Controls.Add(this.label4);
this.Controls.Add(this.searchOptionCB);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.ObjectView);
this.Controls.Add(this.QueryButton);
this.Controls.Add(this.ExitButton);
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "ADQForm";
this.Text = "Querying Active Directory Service";
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.groupBox3.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new ADQForm());
}
private void OnComputersCheckedChanged(object sender, System.EventArgs e)
{
ComputersFilter.Enabled = ComputersCB.Checked;
FilterLabel2.Enabled = ComputersCB.Checked;
if (ComputersCB.Checked)
RootNode.Nodes.Add(ComputersNode);
else
RootNode.Nodes.Remove(ComputersNode);
ObjectView.ExpandAll();
}
private void OnUsersCheckedChanged(object sender, System.EventArgs e)
{
UsersFilter.Enabled = UsersCB.Checked;
FilterLabel1.Enabled = UsersCB.Checked;
if (UsersCB.Checked)
RootNode.Nodes.Add(UsersNode);
else
RootNode.Nodes.Remove(UsersNode);
ObjectView.ExpandAll();
}
private void OnPrintersCheckedChanged(object sender, System.EventArgs e)
{
PrintersFilter.Enabled = PrintersCB.Checked;
FilterLabel3.Enabled = PrintersCB.Checked;
if (PrintersCB.Checked)
RootNode.Nodes.Add(PrintersNode);
else
RootNode.Nodes.Remove(PrintersNode);
ObjectView.ExpandAll();
}
// this function gets the immediate domain server name and adds it to tree
private void GetRootNode()
{
// get the domain server for this computer
DirectoryEntry de = new DirectoryEntry();
// prepare a tree node with this domain controller name
// the original name contains "DC=name"
// remove the string DC=
RootNode = new TreeNode(de.Name.Substring(3), 1, 1);
// add the node to the tree view
ObjectView.Nodes.Add(RootNode);
}
// form a filter string for the search in LDAP format
private string FormFilter(string objectCategory, string filter)
{
String result;
result = String.Format("(&(objectCategory={0})(name={1}))", objectCategory, filter);
return result;
}
// this function forms the filter string based on the selected
// objects on the form
private string GetFilterString()
{
// form the filter string for directory search
string filter = "";
if (UsersCB.Checked)
filter += FormFilter("user", UsersFilter.Text);
if (ComputersCB.Checked)
filter += FormFilter("computer", ComputersFilter.Text);
if (PrintersCB.Checked)
filter += FormFilter("printQueue", PrintersFilter.Text);
// add all the above filter strings
return "(|" + filter + ")";
}
// This function checks the type of object in the DirectoryEntry
// and adds it to the object tree on the main form
public void AddObjectToTree(DirectoryEntry de)
{
// before we add the name, remove the CN= from name
if (String.Compare(de.SchemaClassName, "User", true)==0)
{
UsersNode.Nodes.Add(new TreeNode(de.Name.Substring(3)));
UsersNode.Text = String.Format("Users ({0})", UsersNode.GetNodeCount(false));
}
if (String.Compare(de.SchemaClassName, "Computer", true)==0)
{
ComputersNode.Nodes.Add(new TreeNode(de.Name.Substring(3)));
ComputersNode.Text = String.Format("Computers ({0})", ComputersNode.GetNodeCount(false));
}
if (String.Compare(de.SchemaClassName, "printQueue", true)==0)
{
PrintersNode.Nodes.Add(new TreeNode(de.Name.Substring(3)));
PrintersNode.Text = String.Format("Printers ({0})", PrintersNode.GetNodeCount(false));
}
ObjectView.Update();
}
// Query Active Directory objects using .NET system.DirectoryServices
private void QueryObjectsByNETClasses()
{
DirectorySearcher ds = new DirectorySearcher();
ds.SearchRoot = new DirectoryEntry(""); // start searching from local domain
ds.Filter = GetFilterString(); // get the LDAP filter string based on selections on the form
ds.PropertyNamesOnly = true; // this will get names of only those properties to which a value is set
ds.PropertiesToLoad.Add("name");
// (PageSize) Maximum number of objects the server will return per page
// in a paged search. Default is 0, i.e. no paged search
if (ObjsPerPage.Text.Length > 0)
ds.PageSize = Int32.Parse(ObjsPerPage.Text);
// (ServerPageTimeLimit) the amount of time the server should observe to search a page of results
// default is -1, i.e. search indefinitely
if (PageTimeLimit.Text.Length > 0)
ds.ServerPageTimeLimit = new TimeSpan((long)(Decimal.Parse(PageTimeLimit.Text) * TimeSpan.TicksPerSecond));
// (SizeLimit) maximum number of objects the server returns in a search
// default is 0 - interpreted as server set default limit of 1000 entries
if (ObjsToFetch.Text.Length > 0)
ds.SizeLimit = Int32.Parse(ObjsToFetch.Text);
// (ServerTimeLimit) amount of time that the server should observe in a search
// default is -1 interpreted as server default limit of 120 seconds
if (TotalTimeLimit.Text.Length > 0)
ds.ServerTimeLimit = new TimeSpan((long)(Decimal.Parse(TotalTimeLimit.Text) * TimeSpan.TicksPerSecond));
// (SearchScope) option to search one level or complete subtree
// default is Subtree, so set this option only if oneLevel is selected
if (searchOptionCB.SelectedIndex == 1)
ds.SearchScope = SearchScope.OneLevel;
// (CacheResults) property by default is true
ds.CacheResults = CacheResultsCB.Checked;
ds.ReferralChasing = ReferralChasingOption.None;
if (SortResultsCB.Checked)
ds.Sort = new SortOption("name", SortDirection.Ascending);
// change the cursor to wait cursor
Cursor currentCursor = Cursor.Current;
Cursor.Current = Cursors.WaitCursor;
// start searching
SearchResultCollection src = ds.FindAll();
try
{
foreach (SearchResult sr in src)
AddObjectToTree(sr.GetDirectoryEntry());
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
src.Dispose();
ds.Dispose();
Cursor.Current = currentCursor;
}
private void OnExit(object sender, System.EventArgs e)
{
this.Close();
}
private void OnQuery(object sender, System.EventArgs e)
{
// if no option is checked then quit
if (! (UsersCB.Checked || ComputersCB.Checked || PrintersCB.Checked))
return;
// clean the object tree
UsersNode.Nodes.Clear();
ComputersNode.Nodes.Clear();
PrintersNode.Nodes.Clear();
ObjectView.Update();
ObjectView.ExpandAll();
QueryObjectsByNETClasses();
}
// verify if a positive number is entered in a text box
private void OnValidatingText(object sender, System.ComponentModel.CancelEventArgs e)
{
TextBox t = (TextBox)sender;
if (t.Text.Length == 0) return;
try
{
if (Decimal.Parse(t.Text) < 0) throw new Exception();
}
catch (Exception)
{
MessageBox.Show("Invalid entry. Please enter a correct value");
e.Cancel = true;
}
}
}
}