讀取目錄物件的屬性

在擷取物件的屬性值時,會以可列舉的集合傳回資料,即使只傳回單一值也一樣。此操作使用 Properties 執行,它是 DirectoryEntry 類別上的屬性。Properties 會傳回 PropertyCollection 物件。PropertyCollection 所參照屬性的值儲存在 PropertyValueCollection 物件中。

注意:
Properties 屬性不支援搭配 Active Directory Client Extension (DSClient) for Windows NT 4.0 的用法。

使用 Value 屬性 (來自 PropertyValueCollection 物件) 讀取集合中的屬性值。如果集合中只有一個值,就會以物件傳回該值。

若要存取物件屬性值,請使用下列程式碼範例所示的語法提供屬性的名稱。

DirectoryEntry.Properties("givenName").Value
DirectoryEntry.Properties["givenName"].Value;

在此範例中,程式碼將存取 givenName 屬性,它是 Active Directory 網域服務與其他 LDAP 目錄中使用者物件屬性的 LDAP 顯示名稱。若要存取目錄中的特定屬性,請在應用程式中提供該屬性的 LDAP 顯示名稱。如需 givenName 屬性 (Property) 與 Active Directory 使用者物件的詳細資訊,請參閱 Given-Name 屬性 (Attribute) (本頁面可能為英文) 主題與 User 類別 (本頁面可能為英文) 主題,這些主題位於 MSDN Library,網址為 https://go.microsoft.com/fwlink/?LinkID=27252 (本頁面可能為英文)。

下列程式碼範例示範如何使用 Properties 集合來讀取單一值。

Dim ent As New DirectoryEntry("LDAP://Fabrikam/CN=My Username,CN=Users,DC=Fabrikam,DC=com")
Dim name As [string] = ent.Properties("sn").Value.ToString()
Console.WriteLine(name)
DirectoryEntry ent = new DirectoryEntry("LDAP://Fabrikam/CN=My Username,CN=Users,DC=Fabrikam,DC=com");
string name = ent.Properties["sn"].Value.ToString();
Console.WriteLine(name);

請參閱

參考

System.DirectoryServices
DirectoryEntry
PropertyCollection
PropertyValueCollection

概念

目錄物件屬性

Send comments about this topic to Microsoft.

Copyright © 2007 by Microsoft Corporation.All rights reserved.