在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類的一些基本用法,你可以根據自己的需求進一步探索該類的功能。