Example of DirectoryEntry component application in C #

Source: Internet
Author: User
Tags ldap
In C #, The DirectoryEntry component application instance DirectoryEntry class encapsulates nodes or objects in the Active Directory hierarchy. You can use this class to bind to objects or read and update attributes. Figure 1 shows the DirectoryEntry component.

DirectoryEntry component
1. Functions
The DirectoryEntry class encapsulates nodes or objects in the Active Directory hierarchy. You can use this class to bind to an object or read and update attributes. Figure 1 shows the DirectoryEntry component.

Figure 1 DirectoryEntry component
2. Attributes
Common attributes and descriptions of the DirectoryEntry component are shown in table 1.

Table 1 common attributes and descriptions of the DirectoryEntry component
The following describes important attributes in detail.
Path attribute: used to obtain or set the directory of the DirectoryEntry object. The default value is a null string ("").
Syntax:

Public string Path {get; set ;}

Property Value: directory of the DirectoryEntry object. The default value is an empty string ("").
Example
Use of the Path attribute
This example mainly sets the Path attribute and adds the user name and Working Group on the local machine to the treeview control. The running result 2 is shown.

Figure 2 Path attributes
The main code of the program is as follows:

The complete program code is as follows:
★★★★★Complete program code of the main program file★★★★★

Using System;
Using System. Collections. Generic;
Using System. Windows. Forms;
Namespace _ 8_26
{
Static class Program
{
/// <Summary>
/// Main entry point of the application.
/// </Summary>
[STAThread]
Static void Main ()
{
Application. EnableVisualStyles ();
Application. SetCompatibleTextRenderingDefault (false );
Application. Run (new frmDirectoryEntry ());
}
}
}

★★★★★Complete program code of the frmDirectoryEntry form design File★★★★★

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 ();
}
// The following functions add paths and attributes:
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"; // name of the ZHY computer in which the Workgroup computer is located
// 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 // a required designer variable. /// Summary private System. ComponentModel. IContainer components = null; // summary // clear all

 

Namespace _ 8_26
{
Partial class frmDirectoryEntry
{
/// <Summary>
/// Required designer variables.
/// </Summary>
Private System. ComponentModel. IContainer components = null;
/// <Summary>
/// Clear all resources in use.
/// </Summary>
/// <Param name = "disposing"> If the managed resource should be released, the value is true; otherwise, the value is false. </Param>
Protected override void Dispose (bool disposing)
{
If (disposing & (components! = Null ))
{
Components. Dispose ();
}
Base. Dispose (disposing );
}
# Region code generated by Windows Form Designer
/// <Summary>
/// The designer supports the required methods-do not
/// Use the code editor to modify the content of this method.
/// </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;
}
}

The syntax of the Path attribute varies with the provider. Some common situations are as follows.
(1) WinNT.
① Connect to the group on the computer. For example, "WinNT: // <Domain Name>/<computer name>/<Group Name> ". If you are connecting to a local computer, it is "WinNT: // <computer name>/<Group Name> ".
② Connect to the user on the computer. For example, "WinNT: // <Domain Name>/<computer name>/<User Name> ". If you are connecting to a local computer, it is "WinNT: // <computer name>/<User Name> ".
③ Connect to services on the computer. For example, "WinNT: // <Domain Name>/<computer name>/<service name> ". If you are connecting to a local computer, it is "WinNT: // <computer name>/<service name> ".
④ All domains on the network are found. For example, "WinNT :". You can find these fields by enumerating the sublevel of this item.
(2) LDAP.
① Connect to the group in the domain. For example, "LDAP: // CN = <Group Name>, CN = <user>, DC = <Domain Controller 1>, DC = <Domain Controller 2> ,... ".
② Connect to users in the domain. For example, "LDAP: // CN = <full User Name>, CN = <user>, DC = <Domain Controller 1>, DC = <Domain Controller 2> ,... ".
③ Connect to the computer in the domain. For example, "LDAP: // CN = <computer name>, CN = <computer>, DC = <Domain Controller 1>, DC = <Domain Controller 2> ,... ".
(3) IIS.
① Connect to the Web directory. For example, "IIS: // LocalHost/W3SVC/1/ROOT/<Web directory NAME> ".
② To bind LDAP to the current domain, use the path "LDAP: // RootDSE", obtain the default naming context, and re-bind the item.
3. Method
Exists method: used to determine whether the specified path represents the actual item of the directory.
Syntax:

Public static bool Exists (string path)

 

 

Path: the path of the item to be verified. Return Value: True if the specified path represents the actual item of the Directory Service; otherwise, False.

Path: the path of the item to be verified.
Return Value: True if the specified path represents the actual item of the Directory Service; otherwise, False.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.