C#中 DirectoryEntry組件應用實例
DirectoryEntry組件
1. 功能
DirectoryEntry類封裝Active Directory層次結構中的節點或對象,使用該類可以綁定到對象,或者讀取和更新屬性。圖1所示為DirectoryEntry組件。
圖1 DirectoryEntry組件
2.屬性
DirectoryEntry組件常用屬性及說明如表1所示。
表1 DirectoryEntry組件常用屬性及說明
下面對比較重要的屬性進行詳細介紹。
Path屬性:用於獲取或設置DirectoryEntry對象的路徑,默認值為空字符串(“”)。
語法:
public string Path { get; set; } |
示例
Path屬性的使用
本示例主要是設置Path屬性,將本機上的用戶名、工作組添加到treeview控件中。其運行結果如圖2所示。

圖2 Path屬性
程序主要代碼如下:
★★★★★主程序文件完整程序代碼★★★★★
using System; using System.Collections.Generic; using System.Windows.Forms; namespace _8_26 { static class Program { /// <summary> /// 應用程序的主入口點。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new frmDirectoryEntry()); } } } |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.DirectoryServices; using System.Diagnostics; namespace _8_26 { public partial class frmDirectoryEntry : Form { public frmDirectoryEntry() { InitializeComponent(); } //以下函數實現路徑及屬性的添加功能 private void AddPathAndProperties(TreeNode node, DirectoryEntry entry) { node.Nodes.Add(new TreeNode("Path:" + entry.Path)); TreeNode propertyNode = new TreeNode("Properties"); 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)); } } private void frmDirectoryEntry_Load(object sender, EventArgs e) { //entryPC.Path = "WinNT://192.168.1.96/ZHY"; entryPC.Path = "WinNT://workgroup/Localhost";//Workgroup計算機所處的組//ZHY計算機名 //entryPC.Path = "LDAP://ZHY/rootDSE"; TreeNode users = new TreeNode("Users"); TreeNode groups = new TreeNode("Groups"); TreeNode services = new TreeNode("Services"); viewPC.Nodes.AddRange(new TreeNode[] { users, groups, services }); foreach (DirectoryEntry child in entryPC.Children) { TreeNode newNode = new TreeNode(child.Name); switch (child.SchemaClassName) { case "User": users.Nodes.Add(newNode); break; case "Group": groups.Nodes.Add(newNode); break; case "Service": services.Nodes.Add(newNode); break; } AddPathAndProperties(newNode, child); //http://www.isstudy.com } } }// } |
namespace _8_26 { partial class frmDirectoryEntry { /// <summary> /// 必需的設計器變量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的資源。 /// </summary> /// <param name="disposing">如果應釋放托管資源,為 true;否則為 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗體設計器生成的代碼 /// <summary> /// 設計器支持所需的方法 - 不要 /// 使用代碼編輯器修改此方法的內容。 /// </summary> private void InitializeComponent() { this.entryPC = new System.DirectoryServices.DirectoryEntry(); this.viewPC = new System.Windows.Forms.TreeView(); this.SuspendLayout(); // // viewPC // this.viewPC.Location = new System.Drawing.Point(44, 26); this.viewPC.Name = "viewPC"; this.viewPC.Size = new System.Drawing.Size(195, 97); this.viewPC.TabIndex = 0; // // frmDirectoryEntry // this.ClientSize = new System.Drawing.Size(292, 184); this.Controls.Add(this.viewPC); this.Name = "frmDirectoryEntry"; this.Load += new System.EventHandler(this.frmDirectoryEntry_Load); this.ResumeLayout(false); } #endregion private System.DirectoryServices.DirectoryEntry entryPC; private System.Windows.Forms.TreeView viewPC; } } |
Path屬性的語法將會隨着提供程序的不同而不同。一些常見的情況如下所示。
(1)WinNT。
① 連接到計算機上的組。例如,“WinNT://<域名>/<計算機名>/<組名>”。如果是連接到本地計算機,則為“WinNT://<計算機名>/<組名>”。
② 連接到計算機上的用戶。例如,“WinNT://<域名>/<計算機名>/<用戶名>”。如果是連接到本地計算機,則為“WinNT://<計算機名>/<用戶名>”。
③ 連接到計算機上的服務。例如,“WinNT://<域名>/<計算機名>/<服務名>”。如果是連接到本地計算機,則為“WinNT://<計算機名>/<服務名>”。
④ 發現網絡上的所有域。例如,“WinNT:”。通過枚舉此項的子級可以找到這些域。
(2)LDAP。
① 連接到域中的組。例如,“LDAP://CN=<組名>, CN =<用戶>, DC=<域控制器 1>, DC=<域控制器 2>,...”。
② 連接到域中的用戶。例如,“LDAP://CN=<完整用戶名>, CN=<用戶>, DC=<域控制器 1>, DC=<域控制器 2>,...”。
③ 連接到域中的計算機。例如,“LDAP://CN=<計算機名>, CN=<計算機>, DC=<域控制器 1>, DC=<域控制器 2>,...”。
(3)IIS。
① 連接到Web目錄。例如,“IIS://LocalHost/W3SVC/1/ROOT/<Web 目錄名>”。
② 若要使用LDAP綁定到當前域,請使用路徑“LDAP://RootDSE”,然后獲取默認命名上下文,並重新綁定該項。
3.方法
Exists方法:用於確定指定路徑是否表示目錄的實際項。
語法:
public static bool Exists (string path) |
path:要驗證項的路徑。
返回值:如果指定路徑表示目錄服務的實際項,則為True;否則為False。