一个不错的活动目录察看工具的代码

此博客展示了SimpleAdBrowser项目的代码,包含AdBrowserForm类的定义,涉及TreeView、ListView、ImageList等控件的使用,还定义了ListViewColumnSorter等比较器类,用于对ListView的列进行排序,实现了不同排序规则和比较逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using System.DirectoryServices;

namespace SimpleAdBrowser
{
 /// <summary>
 /// Summary description for Form1.
 /// </summary>
 public class AdBrowserForm : System.Windows.Forms.Form
 {
  private enum AdImages
  {
   AdRoot,
   Ou,
   Container,
   OpenContainer,
   Computer,
   User,
   Group,
   Unknown,
   Unavailable
  }

  private DirectoryEntry _AdRootDSE = null;
  private DirectoryEntry _AdRoot = null;

  // column sorter for the listview
  private ListViewColumnSorter lvwColumnSorter;

  private System.Windows.Forms.TreeView treeView_ad;
  private System.Windows.Forms.ListView listView_ad;
  private System.Windows.Forms.ImageList imageList_adObjects;
  private System.Windows.Forms.Splitter splitter_mainForm;
  private System.Windows.Forms.ColumnHeader columnHeader_name;
  private System.Windows.Forms.ColumnHeader columnHeader_type;
  private System.Windows.Forms.ColumnHeader columnHeader_description;
  private System.ComponentModel.IContainer components;

  public AdBrowserForm()
  {
   //
   // Required for Windows Form Designer support
   //
   InitializeComponent();

   //
   // TODO: Add any constructor code after InitializeComponent call
   //
   myInitializeComponent();
  }

  /// <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()
  {
   this.components = new System.ComponentModel.Container();
   System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(AdBrowserForm));
   System.Windows.Forms.ListViewItem listViewItem1 = new System.Windows.Forms.ListViewItem(new string[] {
                              "Computers",
                              "container"}, 2, System.Drawing.SystemColors.WindowText, System.Drawing.SystemColors.Window, new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))));
   System.Windows.Forms.ListViewItem listViewItem2 = new System.Windows.Forms.ListViewItem(new string[] {
                              "Ou1",
                              "organizationalUnit",
                              "my first organizational unit"}, 1, System.Drawing.SystemColors.WindowText, System.Drawing.SystemColors.Window, new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))));
   System.Windows.Forms.ListViewItem listViewItem3 = new System.Windows.Forms.ListViewItem(new string[] {
                              "Users",
                              "container"}, 2, System.Drawing.SystemColors.WindowText, System.Drawing.SystemColors.Window, new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))));
   this.treeView_ad = new System.Windows.Forms.TreeView();
   this.imageList_adObjects = new System.Windows.Forms.ImageList(this.components);
   this.splitter_mainForm = new System.Windows.Forms.Splitter();
   this.listView_ad = new System.Windows.Forms.ListView();
   this.columnHeader_name = new System.Windows.Forms.ColumnHeader();
   this.columnHeader_type = new System.Windows.Forms.ColumnHeader();
   this.columnHeader_description = new System.Windows.Forms.ColumnHeader();
   this.SuspendLayout();
   //
   // treeView_ad
   //
   this.treeView_ad.Dock = System.Windows.Forms.DockStyle.Left;
   this.treeView_ad.ImageList = this.imageList_adObjects;
   this.treeView_ad.Location = new System.Drawing.Point(0, 0);
   this.treeView_ad.Name = "treeView_ad";
   this.treeView_ad.Nodes.AddRange(new System.Windows.Forms.TreeNode[] {
                     new System.Windows.Forms.TreeNode("dc=test,dc=com", 0, 0, new System.Windows.Forms.TreeNode[] {
                                               new System.Windows.Forms.TreeNode("Computers", 2, 3),
                                               new System.Windows.Forms.TreeNode("Users", 2, 3, new System.Windows.Forms.TreeNode[] {
                                                                      new System.Windows.Forms.TreeNode("Node6", 5, 5),
                                                                      new System.Windows.Forms.TreeNode("Node7", 5, 5)}),
                                               new System.Windows.Forms.TreeNode("Ou1", 1, 3, new System.Windows.Forms.TreeNode[] {
                                                                    new System.Windows.Forms.TreeNode("ExampleComputer", 4, 4),
                                                                    new System.Windows.Forms.TreeNode("Example Group", 6, 6),
                                                                    new System.Windows.Forms.TreeNode("Unknown object", 7, 7)})})});
   this.treeView_ad.Size = new System.Drawing.Size(230, 364);
   this.treeView_ad.Sorted = true;
   this.treeView_ad.TabIndex = 0;
   this.treeView_ad.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView_ad_AfterSelect);
   //
   // imageList_adObjects
   //
   this.imageList_adObjects.ColorDepth = System.Windows.Forms.ColorDepth.Depth32Bit;
   this.imageList_adObjects.ImageSize = new System.Drawing.Size(16, 16);
   this.imageList_adObjects.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList_adObjects.ImageStream")));
   this.imageList_adObjects.TransparentColor = System.Drawing.Color.Transparent;
   //
   // splitter_mainForm
   //
   this.splitter_mainForm.Location = new System.Drawing.Point(230, 0);
   this.splitter_mainForm.Name = "splitter_mainForm";
   this.splitter_mainForm.Size = new System.Drawing.Size(4, 364);
   this.splitter_mainForm.TabIndex = 1;
   this.splitter_mainForm.TabStop = false;
   //
   // listView_ad
   //
   this.listView_ad.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
                        this.columnHeader_name,
                        this.columnHeader_type,
                        this.columnHeader_description});
   this.listView_ad.Dock = System.Windows.Forms.DockStyle.Fill;
   this.listView_ad.Items.AddRange(new System.Windows.Forms.ListViewItem[] {
                      listViewItem1,
                      listViewItem2,
                      listViewItem3});
   this.listView_ad.Location = new System.Drawing.Point(234, 0);
   this.listView_ad.Name = "listView_ad";
   this.listView_ad.Size = new System.Drawing.Size(486, 364);
   this.listView_ad.SmallImageList = this.imageList_adObjects;
   this.listView_ad.Sorting = System.Windows.Forms.SortOrder.Ascending;
   this.listView_ad.TabIndex = 2;
   this.listView_ad.View = System.Windows.Forms.View.Details;
   this.listView_ad.DoubleClick += new System.EventHandler(this.listView_ad_DoubleClick);
   this.listView_ad.ColumnClick += new System.Windows.Forms.ColumnClickEventHandler(this.listView_ad_ColumnClick);
   this.listView_ad.SelectedIndexChanged += new System.EventHandler(this.listView_ad_SelectedIndexChanged);
   //
   // columnHeader_name
   //
   this.columnHeader_name.Text = "Name";
   this.columnHeader_name.Width = 150;
   //
   // columnHeader_type
   //
   this.columnHeader_type.Text = "Type";
   this.columnHeader_type.Width = 100;
   //
   // columnHeader_description
   //
   this.columnHeader_description.Text = "Description";
   this.columnHeader_description.Width = 150;
   //
   // AdBrowserForm
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(720, 364);
   this.Controls.Add(this.listView_ad);
   this.Controls.Add(this.splitter_mainForm);
   this.Controls.Add(this.treeView_ad);
   this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
   this.MinimumSize = new System.Drawing.Size(608, 365);
   this.Name = "AdBrowserForm";
   this.Text = "Simple Active Directory Browser";
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// The main entry point for the application.
  /// </summary>
  [STAThread]
  static void Main()
  {
   try
   {
    Application.Run(new AdBrowserForm());
   }
   catch (Exception exc)
   {
    MessageBox.Show("Error occured in application. Need to close. Error was: " + exc.Message);
   }
  }

  private void myInitializeComponent()
  {
   lvwColumnSorter = new ListViewColumnSorter();
   this.listView_ad.ListViewItemSorter = lvwColumnSorter;

   try
   {
    this._AdRootDSE = new DirectoryEntry("
LDAP://rootDSE");
    this._AdRoot = new DirectoryEntry("LDAP://" + (string)this._AdRootDSE.Properties["defaultNamingContext"].Value);
    /*
    foreach(string property in this._AdRoot.Properties.PropertyNames)
    {
     MessageBox.Show(property + " = " + this._AdRoot.Properties[property].Value);
    }*/

    TreeNode root = new TreeNode((string)this._AdRootDSE.Properties["defaultNamingContext"].Value,(int)AdImages.AdRoot,(int)AdImages.AdRoot);
    
    root.Tag = this._AdRoot;
    this.treeView_ad.Nodes.Clear();
    this.treeView_ad.Nodes.Add(root);
   }
   catch
   {
    throw new Exception("Error connecting to AD");
   }
  }

  private void treeView_ad_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
  {
   TreeView myTreeView = (TreeView)sender;
   DirectoryEntry selectedEntry = (DirectoryEntry)e.Node.Tag;

   if (!DirectoryEntry.Exists(selectedEntry.Path))
   {
    e.Node.ImageIndex = (int)AdImages.Unavailable;
    e.Node.SelectedImageIndex = (int)AdImages.Unavailable;
    
    return;
   }

   // cleanup the current node
   this.listView_ad.Items.Clear();
   e.Node.Nodes.Clear();

   myTreeView.BeginUpdate();

   try
   {
    foreach (DirectoryEntry child in selectedEntry.Children)
    {   
     TreeNode tmpNode = null;
     ListViewItem tmpItem = null;

     switch(child.SchemaClassName)
     {
      case "organizationalUnit":
       tmpNode = new TreeNode((string)child.Properties["name"].Value,(int)AdImages.Ou,(int)AdImages.OpenContainer);
       tmpItem = new ListViewItem(new string[] {
                  (string)child.Properties["name"].Value,
                  child.SchemaClassName,
                  (string)child.Properties["description"].Value

                 },(int)AdImages.Ou);
       break;
      case "container":
       tmpNode = new TreeNode((string)child.Properties["name"].Value,(int)AdImages.Container,(int)AdImages.OpenContainer);
       tmpItem = new ListViewItem(new string[] {
                  (string)child.Properties["name"].Value,
                  child.SchemaClassName,
                  (string)child.Properties["description"].Value

                 },(int)AdImages.Container);
       break;
      case "computer":
       //tmpNode = new TreeNode((string)child.Properties["name"].Value,(int)AdImages.Computer,(int)AdImages.Computer);
       tmpItem = new ListViewItem(new string[] {
                  (string)child.Properties["name"].Value,
                  child.SchemaClassName,
                  (string)child.Properties["description"].Value

                 },(int)AdImages.Computer);
       break;
      case "user":
       //tmpNode = new TreeNode((string)child.Properties["name"].Value,(int)AdImages.User,(int)AdImages.User);
       tmpItem = new ListViewItem(new string[] {
                  (string)child.Properties["name"].Value,
                  child.SchemaClassName,
                  (string)child.Properties["description"].Value

                 },(int)AdImages.User);
       break;
      case "group":
       //tmpNode = new TreeNode((string)child.Properties["name"].Value,(int)AdImages.Group,(int)AdImages.Group);
       tmpItem = new ListViewItem(new string[] {
                  (string)child.Properties["name"].Value,
                  child.SchemaClassName,
                  (string)child.Properties["description"].Value

                 },(int)AdImages.Group);
       break;
      default:
       //tmpNode = new TreeNode((string)child.Properties["name"].Value,(int)AdImages.Unknown,(int)AdImages.Unknown);
       tmpItem = new ListViewItem(new string[] {
                  (string)child.Properties["name"].Value,
                  child.SchemaClassName,
                  (string)child.Properties["description"].Value

                 },(int)AdImages.Unknown);
       break;
     }

     // save the directory entry reference in the tag
     if (tmpNode!=null)
     {
      tmpNode.Tag = child;
      e.Node.Nodes.Add(tmpNode);
     }
     if (tmpItem!=null)
     {
      tmpItem.Tag = child;
      this.listView_ad.Items.Add(tmpItem);
     }     
    }
   }
   catch (Exception exc)
   {
    MessageBox.Show(exc.Message);
    // active directory exception ???
   }

   e.Node.Expand();

   myTreeView.EndUpdate();
  }

  private void listView_ad_DoubleClick(object sender, System.EventArgs e)
  {
   ListView myListView = (ListView)sender;

   if (myListView.SelectedItems.Count == 1)
   {
    ListViewItem myListViewItem = myListView.SelectedItems[0];
    DirectoryEntry myObject = (DirectoryEntry)myListViewItem.Tag;
    if (myObject == null)
     return;

    switch (myListViewItem.ImageIndex)
    {
     case (int) AdImages.Ou:
     case (int) AdImages.Container:
      foreach (TreeNode tNode in this.treeView_ad.SelectedNode.Nodes)
      {
       if (tNode.Text.Equals(myListViewItem.Text))
       {
        this.treeView_ad.SelectedNode = tNode;
        if (myListView.Items.Count >0)
        {
         myListView.Focus();
         myListView.Items[0].Selected = true;          
        }
        break;
       }
      }
      break;       
     default:
      MessageBox.Show("You double-clicked another object");
      break;
    }
   }
  }

  /// <summary>
  /// 单击ListView的列标题的事件
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void listView_ad_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
  {
   ListView myListView = (ListView)sender;

   // Determine if clicked column is already the column that is being sorted.
   if ( e.Column == lvwColumnSorter.SortColumn )
   {
    // Reverse the current sort direction for this column.
    if (lvwColumnSorter.Order == SortOrder.Ascending)
    {
     lvwColumnSorter.Order = SortOrder.Descending;
    }
    else
    {
     lvwColumnSorter.Order = SortOrder.Ascending;
    }
   }
   else
   {
    // Set the column number that is to be sorted; default to ascending.
    lvwColumnSorter.SortColumn = e.Column;
    lvwColumnSorter.Order = SortOrder.Ascending;
   }

   // Perform the sort with these new sort options.
   myListView.Sort();
  }

  private void listView_ad_SelectedIndexChanged(object sender, System.EventArgs e)
  {
  
  }
 }
}

using System;
using System.Collections;
using System.Text.RegularExpressions; 
using System.Windows.Forms;

namespace SimpleAdBrowser
{
 /// <summary>
 /// This class is an implementation of the 'IComparer' interface.
 /// </summary>
 public class ListViewColumnSorter : IComparer
 {
  /// <summary>
  /// Specifies the column to be sorted
  /// </summary>
  private int ColumnToSort;
  /// <summary>
  /// Specifies the order in which to sort (i.e. 'Ascending').
  /// </summary>
  private SortOrder OrderOfSort;
  /// <summary>
  /// Case insensitive comparer object
  /// </summary>
  //private CaseInsensitiveComparer ObjectCompare;
  private NumberCaseInsensitiveComparer ObjectCompare;
  private ImageTextComparer FirstObjectCompare;

  /// <summary>
  /// Class constructor.  Initializes various elements
  /// </summary>
  public ListViewColumnSorter()
  {
   // Initialize the column to '0'
   ColumnToSort = 0;

   // Initialize the sort order to 'none'
   //OrderOfSort = SortOrder.None;
   OrderOfSort = SortOrder.Ascending;

   // Initialize the CaseInsensitiveComparer object
   ObjectCompare = new NumberCaseInsensitiveComparer();//CaseInsensitiveComparer();
   FirstObjectCompare = new ImageTextComparer();
  }

  /// <summary>
  /// This method is inherited from the IComparer interface.  It compares the two objects passed using a case insensitive comparison.
  /// </summary>
  /// <param name="x">First object to be compared</param>
  /// <param name="y">Second object to be compared</param>
  /// <returns>The result of the comparison. "0" if equal, negative if 'x' is less than 'y' and positive if 'x' is greater than 'y'</returns>
  public int Compare(object x, object y)
  {
   int compareResult;
   ListViewItem listviewX, listviewY;

   // Cast the objects to be compared to ListViewItem objects
   listviewX = (ListViewItem)x;
   listviewY = (ListViewItem)y;

   if (ColumnToSort == 0)
   {
    compareResult = FirstObjectCompare.Compare(x,y);
   }
   else
   {
    // Compare the two items
    compareResult = ObjectCompare.Compare(listviewX.SubItems[ColumnToSort].Text,listviewY.SubItems[ColumnToSort].Text);
   }

   // Calculate correct return value based on object comparison
   if (OrderOfSort == SortOrder.Ascending)
   {
    // Ascending sort is selected, return normal result of compare operation
    return compareResult;
   }
   else if (OrderOfSort == SortOrder.Descending)
   {
    // Descending sort is selected, return negative result of compare operation
    return (-compareResult);
   }
   else
   {
    // Return '0' to indicate they are equal
    return 0;
   }
  }
   
  /// <summary>
  /// Gets or sets the number of the column to which to apply the sorting operation (Defaults to '0').
  /// </summary>
  public int SortColumn
  {
   set
   {
    ColumnToSort = value;
   }
   get
   {
    return ColumnToSort;
   }
  }

  /// <summary>
  /// Gets or sets the order of sorting to apply (for example, 'Ascending' or 'Descending').
  /// </summary>
  public SortOrder Order
  {
   set
   {
    OrderOfSort = value;
   }
   get
   {
    return OrderOfSort;
   }
  }
   
 }

 public class ImageTextComparer : IComparer
 {
  //private CaseInsensitiveComparer ObjectCompare;
  private NumberCaseInsensitiveComparer ObjectCompare;
       
  public ImageTextComparer()
  {
   // Initialize the CaseInsensitiveComparer object
   ObjectCompare = new NumberCaseInsensitiveComparer();//CaseInsensitiveComparer();
  }

  public int Compare(object x, object y)
  {
   //int compareResult;
   int image1, image2;
   ListViewItem listviewX, listviewY;

   // Cast the objects to be compared to ListViewItem objects
   listviewX = (ListViewItem)x;
   image1 = listviewX.ImageIndex;
   listviewY = (ListViewItem)y;
   image2 = listviewY.ImageIndex;

   if (image1 < image2)
   {
    return -1;
   }
   else if (image1 == image2)
   {
    return ObjectCompare.Compare(listviewX.Text,listviewY.Text);
   }
   else
   {
    return 1;
   }
  }
 }

 public class NumberCaseInsensitiveComparer : CaseInsensitiveComparer
 {
  public NumberCaseInsensitiveComparer ()
  {
   
  }

  public new int Compare(object x, object y)
  {
   if ((x is System.String) && IsWholeNumber((string)x) && (y is System.String) && IsWholeNumber((string)y))
   {
    return base.Compare(System.Convert.ToInt32(x),System.Convert.ToInt32(y));
   }
   else
   {
    return base.Compare(x,y);
   }
  }

  private bool IsWholeNumber(string strNumber)
  {
   Regex objNotWholePattern=new Regex("[^0-9]");
   return !objNotWholePattern.IsMatch(strNumber);
  } 
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值