/// <summary> /// Static members for ADSI. /// </summary> public class Adsi { /// <summary> /// Wrapper for the /// <see cref="System.DirectoryServices.DirectoryEntry.Exists"/> /// method. /// </summary> /// <param name="Path"> /// An LDAP-style query string. /// </param> /// <returns> /// Returns <c>true</c> when a valid entry is found. /// </returns> public static bool EntryExists(string Path) { bool bln = false;
try
{
bln = DirectoryEntry.Exists(Path);
}
catch { return bln; }
return bln;
}
/// <summary>
/// Returns an
/// <see cref="System.Collections.ArrayList"/>
/// of the Active Directory Security Groups
/// for the target
/// <see cref="System.DirectoryServices.DirectoryEntry"/>.
/// </summary>
/// <param name="Path">
/// An LDAP-style query string.
/// </param>
public static ArrayList EntryGroups(string Path)
{
ArrayList al = new ArrayList();
if (!EntryExists(Path)) return al;
using (DirectoryEntry user = new DirectoryEntry(Path))
{
object adsGroups = user.Invoke("Groups");
foreach (object adsGroup in (IEnumerable)adsGroups)
{
using (DirectoryEntry group = new DirectoryEntry(adsGroup))
{
al.Add(group.Name);
}
}
}
return al;
}
/// <summary>
/// Returns a
/// <see cref="System.Collections.SortedList"/>
/// of
/// <see cref="System.DirectoryServices.DirectoryEntry.Properties"/>.
/// </summary>
/// <param name="Path">
/// An LDAP-style query string.
/// </param>
public static SortedList EntryProperties(string Path)
{
SortedList entries = null;
DirectoryEntry entry = null;
if (EntryExists(Path))
{
entry = new DirectoryEntry(Path);
entries = new SortedList();
foreach (string name in entry.Properties.PropertyNames)
{
if (entry.Properties[name].Value is Byte[])
{
entries.Add(name, "[Complex Data Structure]");
}
else
{
entries.Add(name, entry.Properties[name].Value.ToString());
}
}
}
return entries;
}
}