我在搜索活动目录中的组名时面临问题, 下面的链接与我的错误相关,但它没有解决我的问题
DirectorySearcher FindAll SearchResultCollection Count throws COMException
string search = "*" + txtsearch.Text + "*";
if (string.IsNullOrEmpty(txtsearch.Text))
{
search = "*";
}
DirectoryEntry de = null;
try
{
de = new DirectoryEntry(strADPath,strUsreName,strUsrePassword);
}
catch (Exception ex)
{
LogMessageToFile("Fail to set the data for the DirectoryEntry : " + ex.ToString()+ "....Error message"+ ex.Message);
}
DirectorySearcher deSearch = new DirectorySearcher();
deSearch.PropertiesToLoad.Add("objectguid");
deSearch.PropertiesToLoad.Add("cn");
deSearch.SearchRoot = de;
try
{
deSearch.PropertiesToLoad.Add("cn"); deSearch.Filter = "(&(objectClass=group)(cn=" + search + "))";
}
catch (Exception ex)
{
LogMessageToFile("error while passing value to filter value" + ex.ToString()+"....Error message"+ ex.Message);
}
SearchResultCollection results= null;
try
{
results = deSearch.FindAll();
}
catch (Exception ex)
{
LogMessageToFile("Fail to FindAll method :" + ex.ToString() + "....Error message" + ex.Message);
}
DataTable tbGroup = dsGroup.Tables.Add("Groups");
tbGroup.Columns.Add("GroupName");
tbGroup.Columns.Add("GroupGuid");
if (results != null)
{
if (results.Count > 0)
{
foreach (SearchResult Result in results)
{
DataRow rwGroup = tbGroup.NewRow();
try
{
if (Result.Properties["cn"] != null)
{
if (Result.Properties["cn"].Count > 0)
{
rwGroup["GroupName"] = Result.Properties["cn"][0];
}
else
{
LogMessageToFile("Result.Properties count less then 0");
}
}
}
catch (Exception ex)
{
LogMessageToFile("try to obtain Result.Properties :" + ex.ToString() + "....Error message" + ex.Message);
}
try
{
rwGroup["GroupGuid"] = GetObjectIdetifier(Result);
}
catch (Exception ex)
{
LogMessageToFile("Fail to get the data for GroupGuid :" + ex.ToString()+"....Error message" + ex.Message);
}
tbGroup.Rows.Add(rwGroup);
}
listBox1.DataSource = tbGroup;
listBox1.DisplayMember = "GroupName";
listBox1.ValueMember = "GroupGuid";
}
}