设置具有多个值的属性

本主题说明如何使用以下方法设置多值属性:

  • AddPropertyValueCollection 的一个方法,它将其他属性值追加到一个多值属性中。
  • AddRangePropertyValueCollection 的一个方法,它将多个值追加到一个多值属性中。
  • InsertPropertyValueCollection 的一个方法,它按索引位置将属性值插入一个多值属性中。该位置仅设置在客户端上;当将属性提交到目录时,并不能保证它在 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.

版权所有 (C) 2007 Microsoft Corporation。保留所有权利。