My domain is name : xxxxx.xxx.xx
The problem is where to put active directory name?
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);
}
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;
}
}
}
}

New Topic/Question
Reply




MultiQuote




|