更新时间:2023-09-01 16:28:40
通常,应始终在实现 IDisposable
。 DirectoryEntry
和 DirectorySearcher
均实现 IDisposable
。在您的代码示例中,仅第一个 DirectoryEntry
对象被处置。您还需要为 mySearcher
和 directoryObject
添加using块:
As a general rule you should always call Dispose
on types that implement IDisposable
. Both DirectoryEntry
and DirectorySearcher
implement IDisposable
. In your code example only the first DirectoryEntry
object gets disposed. You need to add a using block for mySearcher
and directoryObject
as well:
Using entry As DirectoryEntry = New DirectoryEntry(connectionPrefix)
Using mySearcher = New DirectorySearcher(entry)
'...'
Using directoryObject = result.GetDirectoryEntry()
'...'
End Using
End Using
End Using
您实际上可以通过不使用 GetDirectoryEntry
来减轻服务器的负载,而直接从服务器中检索 distinguishedName以以下方式搜索结果(此代码未经测试,因为我当前不在域中):
You may actually lighten the load on your server a bit by not using GetDirectoryEntry
and instead retrieve "distinguishedName" directly from the search result in the folling way (this code is untested as I am not currently on a domain):
mySearcher.PropertiesToLoad.Add("distinguishedName");
result = mySearcher.FindOne()
'...'
distinguishedName = result.Properties("distinguishedName")(0)