DirectoryEntry是C#中用于表示Active Directory(AD)中的一個(gè)目錄項(xiàng)的類。它提供了一組方法和屬性,用于與AD進(jìn)行交互,如創(chuàng)建、刪除、修改目錄項(xiàng)等。
以下是DirectoryEntry類的一些常用方法和屬性:
構(gòu)造函數(shù):DirectoryEntry類有多個(gè)構(gòu)造函數(shù),用于實(shí)例化目錄項(xiàng)對(duì)象。其中最常用的是使用目錄項(xiàng)的路徑進(jìn)行構(gòu)造,例如:DirectoryEntry entry = new DirectoryEntry(“LDAP://cn=user,dc=domain,dc=com”);
Path屬性:獲取或設(shè)置目錄項(xiàng)的LDAP路徑。
Children屬性:獲取目錄項(xiàng)的子目錄項(xiàng)集合。
Parent屬性:獲取目錄項(xiàng)的父目錄項(xiàng)。
Properties屬性:獲取目錄項(xiàng)的屬性集合。
Invoke方法:調(diào)用目錄項(xiàng)的方法。
DeleteTree方法:刪除目錄項(xiàng)及其下的所有子目錄項(xiàng)。
CommitChanges方法:將對(duì)目錄項(xiàng)的修改保存到AD中。
RefreshCache方法:刷新目錄項(xiàng)的緩存。
NativeObject屬性:獲取目錄項(xiàng)的原生COM對(duì)象。
使用DirectoryEntry類可以進(jìn)行一系列操作,如創(chuàng)建用戶、刪除用戶、修改用戶屬性等。下面是一個(gè)示例:
DirectoryEntry entry = new DirectoryEntry("LDAP://cn=user,dc=domain,dc=com");
entry.Properties["givenName"].Value = "John";
entry.Properties["sn"].Value = "Doe";
entry.CommitChanges();
以上代碼創(chuàng)建了一個(gè)表示AD中用戶的目錄項(xiàng),并設(shè)置了用戶的名和姓,然后調(diào)用CommitChanges方法將修改保存到AD中。
需要注意的是,使用DirectoryEntry類需要引用System.DirectoryServices命名空間。