PropertyValueCollection.Remove Method

Removes a specified property value from this collection.

Namespace:  System.DirectoryServices
Assembly:  System.DirectoryServices (in System.DirectoryServices.dll)

public void Remove(
	Object value
)

Parameters

value
Type: System.Object

ExceptionCondition
ArgumentNullException

The property value is a null reference (Nothing in Visual Basic).

COMException

An error occurred during the call to the underlying interface.

When working with a multi-valued string property value, the Remove method will successfully remove the correct item. However, identifying the correct items by name is difficult with a multi-valued DNWithString property value (as the DNWithString COM class, which is used to store the DNWithString items, has 2 string properties representing the item). The way to remove such items is to find the object in the collection (by looping thru all the items), then call the Remove function with the object's pointer that you just found. This is illustrated in the example below.

// Bind to the AD object
DirectoryEntry myUser = new DirectoryEntry("LDAP://AdServer:389/CN=MyUsername,CN=Users,DC=contoso,DC=com");

// Get the attribute
PropertyValueCollection testAttribute = myUser.Properties["someAttribute"];

// Find the item in the collection that we want to delete
DNWithString dnwsItemToRemove = null;
foreach (DNWithString dnwsItem in testAttribute)
{
    if (dnwsItem.StringValue.Equals("SomeValue"))
    {
        dnwsItemToRemove = dnwsItem;
        break;
    }
}

// Delete it
testAttribute.Remove(dnwsItemToRemove);

// Store the data
myUser.CommitChanges();

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2015 Microsoft