using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.DirectoryServices;
namespace ADSI1
{
/// <summary>
/// Form1 の概要の説明です。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.DirectoryServices.DirectoryEntry directoryEntry1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox tbPath;
private System.Windows.Forms.TreeView treeView1;
/// <summary>
/// 必要なデザイナ変数です。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows フォーム デザイナ サポートに必要です。
//
InitializeComponent();
//
// TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。
//
}
/// <summary>
/// 使用されているリソースに後処理を実行します。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
/// コード エディタで変更しないでください。
/// </summary>
private void InitializeComponent()
{
this.directoryEntry1 = new System.DirectoryServices.DirectoryEntry();
this.button1 = new System.Windows.Forms.Button();
this.tbPath = new System.Windows.Forms.TextBox();
this.treeView1 = new System.Windows.Forms.TreeView();
this.SuspendLayout();
//
// directoryEntry1
//
this.directoryEntry1.Path = "WinNT://uchukamen";
//
// button1
//
this.button1.Location = new System.Drawing.Point(328, 8);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "Get";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// tbPath
//
this.tbPath.Location = new System.Drawing.Point(16, 8);
this.tbPath.Name = "tbPath";
this.tbPath.Size = new System.Drawing.Size(264, 19);
this.tbPath.TabIndex = 1;
this.tbPath.Text = "WinNT://UCHUKAMEN";
//
// treeView1
//
this.treeView1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.treeView1.ImageIndex = -1;
this.treeView1.Location = new System.Drawing.Point(0, 40);
this.treeView1.Name = "treeView1";
this.treeView1.SelectedImageIndex = -1;
this.treeView1.Size = new System.Drawing.Size(416, 144);
this.treeView1.TabIndex = 2;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
this.ClientSize = new System.Drawing.Size(416, 190);
this.Controls.Add(this.treeView1);
this.Controls.Add(this.tbPath);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Active Directory";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// アプリケーションのメイン エントリ ポイントです。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
// カーソルを砂時計にする。
this.Cursor = Cursors.WaitCursor;
// ツリーノードをクリアする。
this.treeView1.Nodes.Clear();
// ツリーノードの操作を開始
this.treeView1.BeginUpdate();
this.directoryEntry1.Path = this.tbPath.Text;
TreeNode users = new TreeNode("Users");
TreeNode groups = new TreeNode("Groups");
TreeNode services = new TreeNode("Services");
TreeNode printQueue = new TreeNode("PrintQueue");
treeView1.Nodes.AddRange(new TreeNode[] { users, groups, services, printQueue });
foreach(DirectoryEntry child in this.directoryEntry1.Children)
{
TreeNode newNode = new TreeNode(child.Name);
switch (child.SchemaClassName)
{
case "User" :
users.Nodes.Add(newNode);
AddPathAndProperties(newNode, child);
break;
case "Group" :
groups.Nodes.Add(newNode);
AddPathAndProperties(newNode, child);
break;
case "Service" :
services.Nodes.Add(newNode);
AddPathAndProperties(newNode, child);
break;
case "PrintQueue" :
printQueue.Nodes.Add(newNode);
AddPathAndProperties(newNode, child);
break;
default:
Console.WriteLine("Unhandled SchemaClassName:" + child.SchemaClassName);
break;
}
}
// ツリーノードの操作を終了
this.treeView1.EndUpdate();
// カーソルを元に戻す。
this.Cursor = Cursors.Default;
}
// プロパティを取得する
private void AddPathAndProperties(TreeNode node, DirectoryEntry entry)
{
TreeNode propertyNode = new TreeNode("Properties");
try
{
node.Nodes.Add(propertyNode);
foreach (string propertyName in entry.Properties.PropertyNames)
{
string oneNode = propertyName + ": " +
entry.Properties[propertyName][0].ToString();
propertyNode.Nodes.Add(new TreeNode(oneNode));
}
}
catch (Exception exc)
{
propertyNode.Nodes.Add(exc.Message);
}
}
}
}
|