Share via


Invoking ADSI Methods

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

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

The following C# example shows how to invoke the IADsUser interface SetPassword method to set a password. For more information about the IADsUser interface or the SetPassword method, see "IADsUser" or "IADsUser::SetPassword" in the MSDN Library at https://go.microsoft.com/fwlink/?LinkID=27252.

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

The following C# example shows how to invoke the IADsUser interface ChangePassword method to change a password. For more information about the IADsUser interface or the ChangePassword method, see "IADsUser" or "IADsUser::ChangePassword" in the MSDN Library at https://go.microsoft.com/fwlink/?LinkID=27252.

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

The following C# example shows how to invoke the IADsGroup interface Members method to retrieve the members of a group. For more information about the IADsGroup interface or the Members method, see "IADsGroup" or "IADsGroup::Members" in the MSDN Library at https://go.microsoft.com/fwlink/?LinkID=27252.

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);
}

See Also

Reference

System.DirectoryServices
DirectoryEntry
TargetInvocationException
COMException
TargetInvocationException

Concepts

Invoking ADSI

Send comments about this topic to Microsoft.

Copyright © 2007 by Microsoft Corporation. All rights reserved.