我需要浏览ActiveDirectory才能选择计算机对象。我发现,有一些容器可以有计算机项目,另一个可以包含用户,组策略等。我只想显示包含计算机的容器。所以我使用此代码检查容器是否包含任何计算机:
public static bool CheckContainsComputers(DirectoryEntry entry)
{
using (DirectorySearcher ds =
new DirectorySearcher(entry, "(objectCategory=computer)", new string[0], SearchScope.Subtree))
{
ds.Asynchronous = true;
ds.SizeLimit = 1;
try
{
SearchResult sr = ds.FindOne();
return (sr == null) ? false : true;
}
catch
{
return false;
}
}
}
问题:
为了减少对此方法的调用次数,我想知道 - 是否可以在不运行DirectoryEntry的情况下找出DirectorySearcher是否可以包含计算机?
是否可以通过DirectorySearcher与SearchScope.OneLevel的一次调用来查找可以拥有计算机和计算机的容器
答案 0 :(得分:0)
1.如果您已有directoryEntry,则无需再次搜索。 我想你想要的是这样的:
if (entry.Properties["objectCategory"].Value.ToString().Contains("Computer"))
return true;
else
return false;
2。 当然!
DirectoryEntry de = new DirectoryEntry("LDAP://myldapserver.com");
DirectorySearcher directorySearcher = new DirectorySearcher(de);
directorySearcher.SearchScope = SearchScope.OneLevel;
directorySearcher.Filter = "(objectCategory=computer)";
SearchResultCollection srCollection = directorySearcher.FindAll();