Xojo Plugin
Xojo Plugin
Close a Remote SFTP File or Directory Handle
See more SFTP Examples
Demonstrates the Chilkat SFtp.CloseHandle method, which closes a remote file or directory handle previously returned by OpenFile or OpenDir. The only argument is the handle.
Background: Every opened handle corresponds to state the server holds, and servers cap how many a session may keep open at once, so a long-running program that forgets to close handles will eventually start failing to open new ones. Closing also ensures buffered writes are committed. Once closed, a handle is invalid and must not be reused.
Chilkat Xojo Plugin Downloads
Dim success As Boolean
success = False
// Demonstrates the SFtp.CloseHandle method, which closes a remote file or directory handle. The
// only argument is the handle returned by OpenFile or OpenDir.
Dim sftp As New Chilkat.SFtp
// Connect, authenticate, and initialize the SFTP subsystem.
Dim port As Int32
port = 22
success = sftp.Connect("sftp.example.com",port)
If (success = False) Then
System.DebugLog(sftp.LastErrorText)
Return
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
password = "mySshPassword"
success = sftp.AuthenticatePw("mySshLogin",password)
If (success = False) Then
System.DebugLog(sftp.LastErrorText)
Return
End If
success = sftp.InitializeSftp()
If (success = False) Then
System.DebugLog(sftp.LastErrorText)
Return
End If
// Open a remote file, obtaining a handle.
Dim handle As String
handle = sftp.OpenFile("subdir/data.txt","readOnly","openExisting")
If (sftp.LastMethodSuccess = False) Then
System.DebugLog(sftp.LastErrorText)
Return
End If
// ... use the handle ...
// Close the handle to release the server-side resource. A handle should not be used after it
// is closed.
success = sftp.CloseHandle(handle)
If (success = False) Then
System.DebugLog(sftp.LastErrorText)
Return
End If
System.DebugLog("Handle closed.")
sftp.Disconnect