简体   繁体   中英

Using statement for directory search or Ldap functionality in vb.net

i am very new to VB.net. Can i use the USING Statement for calling the Directory Entry and Searcher like the below.

Using entry As DirectoryEntry = New DirectoryEntry(String.Format("LDAP://xxx.com.my"))
        entry.AuthenticationType = AuthenticationTypes.Delegation
        entry.Username = username
        entry.Password = password

        Using searcher As New DirectorySearcher(entry)
            searcher.SearchScope = SearchScope.OneLevel
            Try
                Dim results As SearchResult = searcher.FindOne

                success = Not (results Is Nothing)
            Catch ex As Exception
                success = False
                WriteToFile("ValidateADLogin error : " & ex.Message)
            End Try
        End Using
    End Using

System.DirectoryServices.DirectoryEntry and System.DirectoryServices.DirectorySearcher both inherit from System.ComponentModel.Component , which implements the IDisposable interface. So yes, you can use both of them in a Using statement ( IDisposable is required for this).

Normally the compiler will issue an error if no class in the inheritance chain implements IDisposable .

More info:

http://msdn.microsoft.com/en-us/library/system.directoryservices.directoryentry.aspx http://msdn.microsoft.com/en-us/library/system.directoryservices.directorysearcher

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM