Invoking ADSI Methods

If an ADSI interface supports the IDispatch interface, then you can use the DirectoryEntry.Invoke method to access the methods on that interface. This also applies to any ADSI extensions that you might have added in the past. You do not need to include the ADSI library to use the DirectoryEntry.Invoke method.

When an underlying method fails, a TargetInvocationException exception might be thrown. The InnerException property of the TargetInvocationException object is a COMException object that contains information about the actual error that occurred.

The following code example shows how to invoke the IADsUser method SetPassword to set a password.

DirectoryEntry usr = new DirectoryEntry("LDAP://CN=John Smith, DC=Fabrikam,DC=COM");
usr.Invoke("SetPassword", new object[] {SecurelyStoredPassword});

The following code example shows how to invoke the IADsUser method ChangePassword to change a password.

DirectoryEntry usr = new DirectoryEntry("LDAP://CN=John Smith, DC=Fabrikam,DC=COM");
usr.Invoke("ChangePassword", new object[] {SecurelyStoredPassword, NewSecurelyStoredPassword});

The following code example shows how to invoke the IADsGroup method Members to retrieve the members of a group.

DirectoryEntry grpEntry = new DirectoryEntry("LDAP://CN=Enterprise Admins,CN=Users,DC=Fabrikam, DC=com");
object members = grpEntry.Invoke("Members",null);
foreach( object member in (IEnumerable) members)
{
    DirectoryEntry x = new DirectoryEntry(member);
    Console.WriteLine(x.Name);
}