You can use the DirectoryEntry class's PropertyCollection and PropertyValueCollection collections to get an object's attributes. The following example demonstrates how to read and print the attributes of an Active Directory object.
using System;
using System.DirectoryServices;
namespace Chapter12
{
public class DirectoryBinding
{
public DirectoryBinding()
{
DirectoryEntry MyDirectoryObject = new DirectoryEntry();
//Port 389 is LDAP port
MyDirectoryObject.Path =
"LDAP://HMSRevenge:389/OU=Users,DC=Test,DC=COM";
MyDirectoryObject.Username = @"Test\gregmcb";
MyDirectoryObject.Password = @"MyPassword";
//Gets the attributes of an object
PropertyCollection MyAttributes =
MyDirectoryObject.Properties;
foreach(string MyAttributeName in
MyAttributes.PropertyNames)
{
//Gets the values of an object
PropertyValueCollection MyAttributeValues =
MyAttributes[MyAttributeName];
foreach(string MyValue in MyAttributeValues)
{
Console.WriteLine(MyAttributeName +
" = " + MyValue);
}
}
}
}
}
using System;
using System.DirectoryServices;
namespace Chapter12
{
public class ReadingKnownDirectoryObjects
{
public ReadingKnownDirectoryObjects()
{
DirectoryEntry MyObject = new DirectoryEntry();
MyObject.Path =
"LDAP://HMSRevenge/OU=Users,DC=Test,DC=com";
foreach(string MyValue in MyObject.Properties["email"])
Console.WriteLine("Email" + " : " + MyValue);
}
}
}