2012-05-14 16 views
0

私はVB.netの新機能です。以下のようにディレクトリエントリとサーチャーを呼び出すためにUSING文を使用できますか?vb.netでのディレクトリ検索やLdap機能のためのステートメントの使用

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 

答えて

1

System.DirectoryServices.DirectoryEntrySystem.DirectoryServices.DirectorySearcher両方がIDisposableインタフェースを実装System.ComponentModel.Component、継承します。ですから、両方ともUsingステートメントで使用することができます(これにはIDisposableが必要です)。

継承チェーンのクラスがIDisposableを実装していない場合、通常、コンパイラはエラーを発行します。

さらに詳しい情報:

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

関連する問題