C# 哪些ActiveDirectory对象可以包含计算机?
我需要浏览以选择计算机对象。我发现,有些容器可能包含计算机项,而另一些容器可能包含用户、组策略等。我只想显示包含计算机的容器。因此,我使用此代码检查容器是否包含任何计算机:C# 哪些ActiveDirectory对象可以包含计算机?,c#,active-directory,directoryservices,C#,Active Directory,Directoryservices,我需要浏览以选择计算机对象。我发现,有些容器可能包含计算机项,而另一些容器可能包含用户、组策略等。我只想显示包含计算机的容器。因此,我使用此代码检查容器是否包含任何计算机: public static bool CheckContainsComputers(DirectoryEntry entry) { using (DirectorySearcher ds = new DirectorySearcher(entry, "(objectCategory=comput
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;
}
}
}
问题:
为了减少调用此方法的次数,我想知道-是否可以在不运行的情况下找出是否可以包含计算机
是否可以通过一次对的调用来查找可以拥有计算机和计算机的容器
1.如果您已经拥有directoryEntry,则无需再次搜索它。 我想你想要的是这样的:
if (entry.Properties["objectCategory"].Value.ToString().Contains("Computer"))
return true;
else
return false;
二,。
当然
DirectoryEntry de = new DirectoryEntry("LDAP://myldapserver.com");
DirectorySearcher directorySearcher = new DirectorySearcher(de);
directorySearcher.SearchScope = SearchScope.OneLevel;
directorySearcher.Filter = "(objectCategory=computer)";
SearchResultCollection srCollection = directorySearcher.FindAll();
第一个假设包含对事实的验证,即条目是一台计算机。问题中描述了第二个问题。所以,这不是一个答案。