C#中directoryentry怎么使用
文章标签
directoryentry
在C#中,DirectoryEntry类用于表示目录服务中的目录项。你可以使用DirectoryEntry类来创建、删除、修改和搜索目录项。以下是一些常见的DirectoryEntry的用法示例:
- 创建一个新的目录项:
DirectoryEntry entry = new DirectoryEntry("LDAP://cn=John Doe,ou=Users,dc=example,dc=com");
entry.Properties["givenName"].Value = "John";
entry.Properties["sn"].Value = "Doe";
entry.Properties["userPrincipalName"].Value = "john.doe@example.com";
entry.CommitChanges();
- 获取目录项的属性值:
DirectoryEntry entry = new DirectoryEntry("LDAP://cn=John Doe,ou=Users,dc=example,dc=com");
string firstName = entry.Properties["givenName"].Value.ToString();
string lastName = entry.Properties["sn"].Value.ToString();
string email = entry.Properties["userPrincipalName"].Value.ToString();
- 修改目录项的属性值:
DirectoryEntry entry = new DirectoryEntry("LDAP://cn=John Doe,ou=Users,dc=example,dc=com");
entry.Properties["givenName"].Value = "John";
entry.Properties["sn"].Value = "Smith";
entry.CommitChanges();
- 删除目录项:
DirectoryEntry entry = new DirectoryEntry("LDAP://cn=John Doe,ou=Users,dc=example,dc=com");
entry.DeleteTree();
- 搜索目录项:
DirectoryEntry root = new DirectoryEntry("LDAP://dc=example,dc=com");
DirectorySearcher searcher = new DirectorySearcher(root);
searcher.Filter = "(objectClass=user)";
searcher.PropertiesToLoad.Add("cn");
searcher.PropertiesToLoad.Add("givenName");
searcher.PropertiesToLoad.Add("sn");
SearchResultCollection results = searcher.FindAll();
foreach (SearchResult result in results)
{
DirectoryEntry entry = result.GetDirectoryEntry();
string firstName = entry.Properties["givenName"].Value.ToString();
string lastName = entry.Properties["sn"].Value.ToString();
Console.WriteLine("Name: " + firstName + " " + lastName);
}
这些示例只是DirectoryEntry类的一些基本用法,你可以根据自己的需求进一步探索该类的功能。
版权声明
本文仅代表作者观点,不代表米安网络立场。
上一篇:ubuntu中jellyfin使用要注意哪些事项 下一篇:js随机数怎么获取
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。