DataTable dtUser= new DataTable(); try { DirectoryEntry dom = Domain.GetComputerDomain().GetDirectoryEntry(); DirectorySearcher dsAllUsers = new DirectorySearcher(dom); dsAllUsers.SearchScope = SearchScope.Subtree; dsAllUsers.Filter = "(objectCategory=Person)"; SearchResultCollection result = dsAllUsers.FindAll(); dtUser.Columns.Add("CustodianName"); dtUser.Columns.Add("Email"); dtUser.Columns.Add("Title"); dtUser.Columns.Add("Dept."); foreach (SearchResult rs in result) { DataRow newRow = dtUser.NewRow(); if (rs.GetDirectoryEntry().Properties["samaccountname"].Value != null) newRow["CustodianName"] = rs.GetDirectoryEntry().Properties["samaccountname"].Value.ToString(); if (rs.GetDirectoryEntry().Properties["mail"].Value != null) newRow["Email"] = rs.GetDirectoryEntry().Properties["mail"].Value.ToString(); if (rs.GetDirectoryEntry().Properties["title"].Value != null) newRow["Title"] = rs.GetDirectoryEntry().Properties["title"].Value.ToString(); if (rs.GetDirectoryEntry().Properties["department"].Value != null) newRow["Dept."] = rs.GetDirectoryEntry().Properties["department"].Value.ToString(); dtUser.Rows.Add(newRow); } return dtUser; } catch (Exception) { throw; }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)