Last post Jun 24, 2007 11:15 AM by Novice.Net
Jun 24, 2007 08:23 AM|Novice.Net|LINK
I am new to VB.Net and in the past have used VBS. There I would use ADO to search directories and try to pass the ADODB object between searches rather than destroying and re-intiating between calls. The Directory Searcher class looks much the same but I
want to make sure I am using if efficently.
As an example if I want to search my Exchange 5.5 Directory then a couple of Active Directory Forests (we have 4 Forests, plus two iPlanet Directories)
1 'Create .Net Directory Search Object
2 Dim DirEntry
As New DirectoryServices.DirectoryEntry(LDAPPath)
3 Dim SearchDir As New DirectoryServices.DirectorySearcher(DirEntry)
4 SearchDir.Filter = "(&(uid=" & alias to search for &
6 'one dimensional string array to set the attribute list of the search
9 'Create Search Collection and Result to hold values from query
10 Dim searchReturn
11 Dim SearchResult As DirectoryServices.SearchResult
It looks to me that I can't carry anything between searches becase the LDAPPath for the DirectoryServices.DirectoryEntry is different and this is the route object. However, can I refresh the object rather than destryoing and recreating? What about the other
parameters? The Search Filter will be different (Exchange 5.5 I am using UID (which is alias) whilst in Active Directory it becomes mailnickname). The collection of attributes that I want to return will also be different because of the different naming conventions
Finally, how do I clear up properly at the end of the procedure? Again from my VBS experience I would use a combination of 'object.close' and 'Set object = Nothing'?
Jun 24, 2007 10:58 AM|vcsjones|LINK
Each DirectoryEntry wraps one AdsObject, so I don't think there is a way to preserve it or reuse it.
The proper way to do cleanup is to call Dispose() on your Searcher and DirectoryEntry when you are done using them. You don't have to set them to Nothing. However usually you will notice problems with the DirectoryEntry if you don't call dispose. The most
common exceptions are CannotUnloadAppDomain and random ComExceptions.
Jun 24, 2007 11:15 AM|Novice.Net|LINK
Thanks for that it has confirmed what I was assuming. The clarification on Dispose is handy as well.
I found this useful article on the Technet site that is now in my favorites (sic) as it gives good advice on building efficient filters for LDAP: