Invoking ADSI Properties

The properties of an ADSI COM object can be accessed directly in one of two ways. The first way to access it is to use the InvokeMember method. The second way to access it is to use the InvokeGet and InvokeSet methods, which are new with Microsoft .NET Framework version 2.0.

The following C# example shows how to use the InvokeMember method to retrieve the IADSUser property, FirstName property, and LastName property from a managed code application. For more information about these properties, see the topics "IADsUser", "FirstName", and "LastName" in the MSDN Library at https://go.microsoft.com/fwlink/?LinkID=27252.

using System.Reflection;
using System.DirectoryServices;

DirectoryEntry ent = new DirectoryEntry("LDAP://CN=My User,DC=Fabrikam,DC=com");
Object ads = ent.NativeObject;
Type type = ads.GetType();
String firstName = (string)type.InvokeMember(
    "FirstName", 
    BindingFlags.GetProperty, 
    null, 
    ads, 
    null);
String lastName = (string)type.InvokeMember(
    "LastName", 
    BindingFlags.GetProperty, 
    null, 
    ads, 
    null);

The following Visual Basic example shows how to use the InvokeMember method to retrieve the IADSUser property, FirstName property, and LastName property from a managed code application. For more information about these properties, see the topics "IADsUser", "FirstName", and "LastName" in the MSDN Library at https://go.microsoft.com/fwlink/?LinkID=27252.

Imports System.Reflection
Imports System.DirectoryServices

Dim ent As New DirectoryEntry("LDAP://CN=My User,DC=Fabrikam,DC=com")
Dim ads As [Object] = ent.NativeObject
Dim type As Type = ads.GetType()
Dim firstName As String = CStr(type.InvokeMember( _
    "FirstName", _
    BindingFlags.GetProperty, _
    Nothing, _
    ads, _
    Nothing))
Dim lastName As String = CStr(type.InvokeMember( _
    "LastName", _
    BindingFlags.GetProperty, _
    Nothing, _
    ads, _
    Nothing))

The following C# example shows how to use the InvokeGet method to retrieve the IADSUser property, FirstName property, and LastName property from a managed code application. For more information about these properties, see the topics "IADsUser", "FirstName", and "LastName" in the MSDN Library at https://go.microsoft.com/fwlink/?LinkID=27252.

.

using System.DirectoryServices;

DirectoryEntry ent = new DirectoryEntry("LDAP://CN=My User,DC=Fabrikam,DC=com");
String firstName = (string)ent.InvokeGet("FirstName");
String lastName = (string)ent.InvokeGet("LastName");

The following Visual Basic example shows how to use the InvokeGet method to retrieve the IADSUser property, FirstName property, and LastName property from a managed code application. For more information about these properties, see the topics "IADsUser", "FirstName", and "LastName" in the MSDN Library at https://go.microsoft.com/fwlink/?LinkID=27252.

Imports System.DirectoryServices

Dim ent As New DirectoryEntry("LDAP://CN=My User,DC=Fabrikam,DC=com")
Dim firstName As String = CStr(ent.InvokeGet("FirstName"))
Dim lastName As String = CStr(ent.InvokeGet("LastName"))

The following C# example shows how to use the InvokeMember method to set the Description property of an object.

using System.Reflection;
using System.DirectoryServices;

DirectoryEntry ent = new DirectoryEntry("LDAP://CN=My User,DC=Fabrikam,DC=com");
Object ads = ent.NativeObject;
Type type = ads.GetType();
type.InvokeMember("Description", 
    BindingFlags.SetProperty, 
    null, 
    ads, 
    new object[] {"some description"});

// The changes to the object must always be committed or else they 
// will be lost.
ent.CommitChanges(); 

The following Visual Basic example shows how to set the Description property of an object using the InvokeMember method.

Imports System.Reflection
Imports System.DirectoryServices

Dim ent As New DirectoryEntry("LDAP://CN=My User,DC=Fabrikam,DC=com")
Dim ads As [Object] = ent.NativeObject
Dim type As Type = ads.GetType()
type.InvokeMember("Description", _
    BindingFlags.SetProperty, _
    Nothing, _
    ads, _
    New Object() {"some description"})

' The changes to the object must always be committed or else they 
' will be lost.
ent.CommitChanges()

The following C# example shows how to set the Description property of a directory entry using the InvokeSet method. For more information about the Description property, see the topic "Description" in the MSDN Library at https://go.microsoft.com/fwlink/?LinkID=27252.

using System.DirectoryServices;

DirectoryEntry ent = new DirectoryEntry("LDAP://CN=My User,DC=Fabrikam,DC=com");
ent.InvokeSet("Description", new object[] {"some description"});

// The changes to the object must always be committed or else they 
// will be lost.
ent.CommitChanges();

The following Visual Basic example shows how to set the Description property of a directory entry using the InvokeSet method. For more information about the Description property, see the topic "Description" in the MSDN Library at https://go.microsoft.com/fwlink/?LinkID=27252.

Imports System.DirectoryServices

Dim ent As New DirectoryEntry("LDAP://CN=My User,DC=Fabrikam,DC=com")
ent.InvokeSet("Description", New Object() {"some description"})

' The changes to the object must always be committed or else they 
' will be lost.
ent.CommitChanges()

See Also

Reference

System.DirectoryServices
DirectoryEntry
Type

Concepts

Invoking ADSI

Send comments about this topic to Microsoft.

Copyright © 2007 by Microsoft Corporation. All rights reserved.