问题描述
我正在使用这段代码来输出我网络上所有计算机的列表(语言是 jscript.net,但它只是对 C# 的一个小操作).
I'm using this snippet of code to output a list of all the computers on my network (the language is jscript.net, but it's just a small manipulation of C#).
var parentEntry = new DirectoryEntry();
parentEntry.Path = "WinNT:";
for(var childEntry in parentEntry.Children) {
if(childEntry.SchemaClassName == "Domain") {
var parentDomain = new TreeNode(childEntry.Name);
this.treeView1.Nodes.Add(parentDomain);
var subChildEntry : DirectoryEntry;
var subParentEntry = new DirectoryEntry();
subParentEntry.Path = "WinNT://" + childEntry.Name;
for(subChildEntry in subParentEntry.Children) {
var newNode1 = new TreeNode(subChildEntry.Name);
if(subChildEntry.SchemaClassName == "Computer") {
parentDomain.Nodes.Add(newNode1);
}
}
}
}
我有两个问题:
1) 非常慢.大约有 100 台计算机显示,加载大约需要 1 分钟.
1) It is extremely slow. There's about 100 computers showing, and it takes about 1 minute to load.
2) 我只想获取当前在线的计算机列表.
2) I want to get only a list of computers that are currently online.
这是可以做到的,因为我见过其他程序这样做,而且它们的速度要快得多,而且它们只能在线显示那些.
This can be done because I've seen other programs doing it and they are much faster, also they're able to show only the ones online.
我错过了什么吗?
推荐答案
我知道它有点老了,但对其他人来说...此代码段将在 2-3 秒内使用 AD 从域中返回 760 个计算机名称...显着的改进..享受!
I know it's a bit old, but for others...this snippet will return a 760 computer names from a domain using AD within 2-3 seconds....a significant improvement....enjoy!
Friend Shared Function DomainComputers() As Generic.List(Of String)
' Add a Reference to System.DirectoryServices
Dim Result As New Generic.List(Of String)
Dim ComputerName As String = Nothing
Dim SRC As SearchResultCollection = Nothing
Dim Searcher As DirectorySearcher = Nothing
Try
Searcher = New DirectorySearcher("(objectCategory=computer)", {"Name"})
Searcher.Sort = New SortOption("Name", SortDirection.Ascending)
Searcher.Tombstone = False
SRC = Searcher.FindAll
For Each Item As SearchResult In SRC
ComputerName = Item.Properties("Name")(0)
Result.Add(ComputerName)
Next
Catch ex As Exception
End Try
Return Result
End Function
这篇关于Active Directory 中联机的计算机列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!