C#中DirectoryEntry对象怎么使用

   2025-02-22 5250
核心提示:DirectoryEntry对象在C#中用于操作和管理目录服务(也称为Active Directory)中的目录项。下面是一些常用的DirectoryEntry对象的

DirectoryEntry对象在C#中用于操作和管理目录服务(也称为Active Directory)中的目录项。下面是一些常用的DirectoryEntry对象的使用方法:

创建一个DirectoryEntry对象:
DirectoryEntry entry = new DirectoryEntry("LDAP://servername/ou=users,dc=example,dc=com", "admin", "password");
获取目录项的属性:
string username = entry.Properties["sAMAccountName"].Value.ToString();string email = entry.Properties["mail"].Value.ToString();
修改目录项的属性:
entry.Properties["displayName"].Value = "John Doe";entry.CommitChanges();
添加新的目录项:
DirectoryEntry newUser = entry.Children.Add("CN=New User", "user");newUser.Properties["sAMAccountName"].Value = "newuser";newUser.Properties["givenName"].Value = "New";newUser.Properties["sn"].Value = "User";newUser.Properties["userPrincipalName"].Value = "newuser@example.com";newUser.CommitChanges();
删除目录项:
entry.Children.Remove("CN=New User");
枚举子目录项:
foreach (DirectoryEntry child in entry.Children){Console.WriteLine(child.Name);}

以上是一些常用的DirectoryEntry对象的使用示例。请注意,使用DirectoryEntry对象操作Active Directory时需要相应的权限。

 
 
更多>同类维修知识
推荐图文
推荐维修知识
点击排行
网站首页  |  关于我们  |  联系方式  |  用户协议  |  隐私政策  |  网站留言