public void GetAllGroups()
{
try
{
StringBuilder strBuilder = new StringBuilder();
DirectoryEntry de = new DirectoryEntry("activedirectorypath", "adminUserName", "adminPassword");
DirectorySearcher deSearch = new DirectorySearcher();
deSearch.SearchRoot = de;
deSearch.Filter = "(&(ObjectClass=group))";
deSearch.PropertiesToLoad.Add("name");
SearchResultCollection results = deSearch.FindAll();
foreach (SearchResult res in results)
{
DisplayProperties("name", res, strBuilder);
}
lblGroups.Text = strBuilder.ToString();
}
catch (Exception ex)
{
}
}
private void DisplayProperties(string property, SearchResult res, StringBuilder strBuilder)
{
if (strBuilder.ToString() != "")
{
strBuilder.Append("<br/>");
}
ResultPropertyValueCollection col = res.Properties[property];
foreach (object o in col)
{
strBuilder.Append(o.ToString());
}
}