共用方式為


設定具有多個值的屬性

本主題示範如何使用下列方法設定多值屬性:

  • Add 是一種 PropertyValueCollection 的方法,可附加額外的屬性值到多值屬性。
  • AddRange 是一種 PropertyValueCollection 的方法,可附加多重值到多值屬性。
  • Insert 是一種 PropertyValueCollection 的方法,藉由其索引位置將屬性值插入多值屬性中。此位置只設定於用戶端;當您將它確認到目錄時,就不保證它將被存入 Active Directory 網域服務中的這個索引位置。

您也可以使用索引陣列來設定這些值。

在設定屬性值時,資料會存至屬性快取中。若要將新資料寫入目錄中,請呼叫 CommitChanges 方法。如需詳細資訊,請參閱屬性快取

下列程式碼範例示範如何使用 AddRange 方法。

Dim ent As New DirectoryEntry("LDAP://Fabrikam/CN=My Username,CN=Users,DC=Fabrikam,DC=com")
ent.Properties("otherTelephone").AddRange(New Object() {"(425) 523 1462", "(523) 125 6321"})
ent.CommitChanges()
DirectoryEntry ent = new DirectoryEntry("LDAP://Fabrikam/CN=My Username,CN=Users,DC=Fabrikam,DC=com");
ent.Properties["otherTelephone"].AddRange(new object[] {"(425) 523 1462","(523) 125 6321"});
ent.CommitChanges();

下列程式碼範例示範如何使用 Insert 方法。

Dim ent As New DirectoryEntry("LDAP://Fabrikam/CN=My Username,CN=Users,DC=Fabrikam,DC=com")
ent.Properties("otherTelephone").Insert(2, "525 623 5423")
ent.CommitChanges()
DirectoryEntry ent = new DirectoryEntry("LDAP://Fabrikam/CN=My Username,CN=Users,DC=Fabrikam,DC=com");
ent.Properties["otherTelephone"].Insert(2, "525 623 5423");
ent.CommitChanges();

下列程式碼範例示範如何使用陣列來設定多值屬性的值。

Dim ent As New DirectoryEntry("LDAP://Fabrikam/CN=My Username,CN=Users,DC=Fabrikam,DC=com")
ent.Properties("otherTelephone")(0) = "425 263 6234"
ent.CommitChanges()
DirectoryEntry ent = new DirectoryEntry("LDAP://Fabrikam/CN=My Username,CN=Users,DC=Fabrikam,DC=com");
ent.Properties["otherTelephone"][0] = "425 263 6234";
ent.CommitChanges();

請參閱

參考

System.DirectoryServices
PropertyValueCollection
DirectoryEntry

概念

具有多個值的屬性

Send comments about this topic to Microsoft.

Copyright © 2007 by Microsoft Corporation.All rights reserved.