日期:2015-03-14  浏览次数:2250 次

DirectoryEntry类封装Active Directory层次结构中的节点或对象,使用该类可以绑定到对象,或者读取和更新属性。图1所示为DirectoryEntry组件。

DirectoryEntry组件
1.  功能
DirectoryEntry类封装Active Directory层次结构中的节点或对象,使用该类可以绑定到对象,或者读取和更新属性。图1所示为DirectoryEntry组件。

图1  DirectoryEntry组件
2.属性
DirectoryEntry组件常用属性及说明如表1所示。

表1       DirectoryEntry组件常用属性及说明
下面对比较重要的属性进行详细介绍。
Path属性:用于获取或设置DirectoryEntry对象的路径,默认值为空字符串(“”)。
语法:

public string Path { get; set; }

属性值:DirectoryEntry对象的路径,默认值为空字符串(“”)。
示例
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());
            }
            }
            }

★★★★★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 /// 清理所有正在


 

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。


path:要验证项的路径。
返回值:如果指定路径表示目录服务的实际项,则为True;否则为False。