first_page the funky knowledge base
personal notes from way, _way_ back and maybe today

C# Code: ADSI Class for .NET 1.1 Active Directory Searching

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

    /// &lt;summary&gt;
    /// Returns an
    /// &lt;see cref=&quot;System.Collections.ArrayList&quot;/&gt;
    /// of the Active Directory Security Groups
    /// for the target
    /// &lt;see cref=&quot;System.DirectoryServices.DirectoryEntry&quot;/&gt;.
    /// &lt;/summary&gt;
    /// &lt;param name=&quot;Path&quot;&gt;
    /// An LDAP-style query string.
    /// &lt;/param&gt;
    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(&quot;Groups&quot;);

            foreach (object adsGroup in (IEnumerable)adsGroups)
            {
                using (DirectoryEntry group = new DirectoryEntry(adsGroup))
                {
                    al.Add(group.Name);
                }
            }
        }
        return al;
    }

    /// &lt;summary&gt;
    /// Returns a
    /// &lt;see cref=&quot;System.Collections.SortedList&quot;/&gt;
    /// of
    /// &lt;see cref=&quot;System.DirectoryServices.DirectoryEntry.Properties&quot;/&gt;.
    /// &lt;/summary&gt;
    /// &lt;param name=&quot;Path&quot;&gt;
    /// An LDAP-style query string.
    /// &lt;/param&gt;
    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, &quot;[Complex Data Structure]&quot;);
                }
                else
                {
                    entries.Add(name, entry.Properties[name].Value.ToString());
                }
            }
        }

        return entries;
    }
}
mod date: 2006-03-08T04:34:22.000Z