DirectoryEntry Basics
The
DirectoryEntry class
contains several properties to access the attributes of Active
Directory objects. The following code shows how to display the
currentTime attribute of the
RootDSE:
Dim objRootDSE As New DirectoryEntry("LDAP://RootDSE")
Console.WriteLine(objRootDSE.Properties("currentTime")(0))In the code, once we instantiated the DirectoryEntry object, we can
access the currentTime attribute by passing it to
the Properties property. The
Properties property actually returns a collection
of values for the attribute in the form of a
PropertyCollection class, which is why we needed
to specify an index of 0 to get at a specific value. If the
currentTime attribute was multivalued, we could
get at the other values by incrementing the index to 1 and so on.
Tip
In object-oriented parlance, a property allows you to get or set an attribute of an object. A method typically results in some kind of action being taken on the object.
Now let’s look at how to display all of the values
for all of the attributes of an object in Active Directory. Again we
will use RootDSE as the object we want to display:
Dim objRootDSE As New DirectoryEntry("LDAP://RootDSE")
Dim strAttrName As String
Dim objValue As Object
For Each strAttrName In objRootDSE.Properties.PropertyNames
For Each objValue In objRootDSE.Properties(strAttrName)
Console.WriteLine(strAttrName & " : " & objValue.ToString)
Next objValue
Next strAttrNameAs you can see, the Properties property, which
returns a PropertyCollection ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access