Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSH.NET UploadFile not releasing file lock

I'm using the SSH.NET SFTP functionality to upload a file to an SFTP. I have written it into a function however the issue I appear to have is after the upload has completed and I try to move the original file I'm getting a File is being used by another process.

I know this is the upload process not releasing the file as I have commented the upload function out and everything works as expected.

Any pointers on my code as to what my issue is would be greatly appreciated.

Private Function UploadFile(ByVal file As String, ByVal branch As String, ByVal policyref As String) As Boolean

    Dim connInfo As New ConnectionInfo("abc:22", "guestuser", New PasswordAuthenticationMethod("guestuser", "password"))

    Using client As New SftpClient(connInfo)

        client.Connect()
        If client.IsConnected Then
            Console.WriteLine("Connected to sFTP")
        End If

        Dim fileStream As New FileStream(file, FileMode.Create)

        client.UploadFile(fileStream, "/Test/" & Path.GetFileName(file))

        If client.Exists("/Test/" & Path.GetFileName(file)) Then
            Console.WriteLine("File Uploaded - " & Path.GetFileName(file))
            UpdateFTPLog(branch, policyref)
            Return True
        End If

        client.Disconnect()
        client.Dispose()

    End Using

    Return False
End Function
like image 548
Lynchie Avatar asked Oct 21 '25 04:10

Lynchie


1 Answers

It has nothing to do with SSH.NET. It is a fault of your own code. You never dispose the fileStream.

It should be:

Using fileStream As FileStream = New FileStream(file, FileMode.Create)
    client.UploadFile(fileStream, "/Test/" & Path.GetFileName(file))
End Using
like image 79
Martin Prikryl Avatar answered Oct 23 '25 21:10

Martin Prikryl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!