VB.NET
VB.NET
Open a Remote Directory on an SFTP Server
See more SFTP Examples
Demonstrates the Chilkat SFtp.OpenDir method, which opens a remote directory and returns a handle for reading its entries. The only argument is the directory path. Close the handle with CloseHandle when finished.
Background: Listing a directory in SFTP is a handle-based operation, mirroring file access: open the directory to get a handle, read its entries, then close it. The handle is passed to
ReadDirListing to retrieve the names and attributes. As with file handles, the directory handle is a server-side resource that should be closed to avoid exhausting the server's limits.Chilkat VB.NET Downloads
Dim success As Boolean = False
' Demonstrates the SFtp.OpenDir method, which opens a remote directory and returns a handle for
' reading its entries. The only argument is the directory path.
Dim sftp As New Chilkat.SFtp
' Connect, authenticate, and initialize the SFTP subsystem.
Dim port As Integer = 22
success = sftp.Connect("sftp.example.com",port)
If (success = False) Then
Debug.WriteLine(sftp.LastErrorText)
Exit Sub
End If
' Normally you would not hard-code the password in source. You should instead obtain it
' from an interactive prompt, environment variable, or a secrets vault.
Dim password As String = "mySshPassword"
success = sftp.AuthenticatePw("mySshLogin",password)
If (success = False) Then
Debug.WriteLine(sftp.LastErrorText)
Exit Sub
End If
success = sftp.InitializeSftp()
If (success = False) Then
Debug.WriteLine(sftp.LastErrorText)
Exit Sub
End If
' Open the remote directory. OpenDir returns a handle string.
Dim handle As String = sftp.OpenDir("subdir")
If (sftp.LastMethodSuccess = False) Then
Debug.WriteLine(sftp.LastErrorText)
Exit Sub
End If
Debug.WriteLine("Opened directory, handle = " & handle)
' The directory entries are read with ReadDirListing using this handle.
' Close the handle when finished.
success = sftp.CloseHandle(handle)
If (success = False) Then
Debug.WriteLine(sftp.LastErrorText)
Exit Sub
End If
sftp.Disconnect()