9

VB2010 with SSH.NET.

I've downloaded and implemented the library to do an SFTP download and it works great. I've been looking at the documentation and examples and just cannot see how to implement an SFTP download with progress. I want to display the progress of the download as it occurs. So far I have:

Imports Renci.SshNet
Imports System.IO

    Using sftp As New SftpClient("0.0.0.0", 25, "id", "pwd")
        'connect to the server
        sftp.Connect()

        'the name of the remote file we want to transfer to the PC
        Dim remoteFileName As String = "/data/OUT/trips.txt"

        'download the file as a memory stream and convert to a file stream
        Using ms As New MemoryStream
            'download as memory stream
            sftp.DownloadFile(remoteFileName, ms)

            'create a file stream
            Dim fs As New FileStream("c:\mytrips.txt", FileMode.Create, FileAccess.Write)

            'write the memory stream to the file stream
            ms.WriteTo(fs)

            'close file stream
            fs.Close()

            'close memory stream
            ms.Close()
        End Using

        'disconnect from the server
        sftp.Disconnect()

        MsgBox("The file has been downloaded from the server.", MsgBoxStyle.Information)
    End Using

Edit: ok I've done some research and found an example in the codeplex discussion forum. Out of that I learned that there is another downloading function which is asynchronous which I will use. Its a good approach to displaying progress in the debug window and also a progressbar control. Feel free to comment.

Imports Renci.SshNet
Imports System.IO
Imports Renci.SshNet.Sftp
Dim fileSize As Long

Private Sub btnGo_Click(sender As Object, e As EventArgs) Handles btnGo.Click
    Try
        Using sftp As New SftpClient("0.0.0.0", 25, "id", "pwd") 
            'connect to the server
            sftp.Connect()

            'the name of the remote file we want to transfer to the PC
            Dim remoteFileName As String = "/Data/OUT/Config.txt"

            'check for existence of the file
            Dim IsExists As Boolean = sftp.Exists(remoteFileName)
            If IsExists Then
                'get the attributes of the file (namely the size)
                Dim att As Sftp.SftpFileAttributes = sftp.GetAttributes(remoteFileName)
                fileSize = att.Size

                'download the file as a memory stream and convert to a file stream
                Using ms As New MemoryStream
                    'download as memory stream
                    'sftp.DownloadFile(remoteFileName, ms, AddressOf DownloadCallback) 'with download progress
                    'sftp.DownloadFile(remoteFileName, ms) 'without download progress

                    'here we try an asynchronous operation and wait for it to complete.
                    Dim asyncr As IAsyncResult = sftp.BeginDownloadFile(remoteFileName, ms)
                    Dim sftpAsyncr As SftpDownloadAsyncResult = CType(asyncr, SftpDownloadAsyncResult)
                    While Not sftpAsyncr.IsCompleted
                        Dim pct As Integer = CInt((sftpAsyncr.DownloadedBytes / fileSize) * 100)
                        Debug.Print("Downloaded {0} of {1} ({2}%).", sftpAsyncr.DownloadedBytes, fileSize, pct)
                        pgbMain.Value = pct

                        Application.DoEvents()
                    End While
                    sftp.EndDownloadFile(asyncr)

                    'create a file stream
                    Dim localFileName As String = "c:\" & Date.Now.ToString("yyyy-dd-MM_HHmmss") & "_test.txt"
                    Dim fs As New FileStream(localFileName, FileMode.Create, FileAccess.Write)

                    'write the memory stream to the file stream
                    ms.WriteTo(fs)

                    'close file stream
                    fs.Close()

                    'close memory stream
                    ms.Close()
                End Using

                'disconnect from the server
                sftp.Disconnect()

                'success
                MsgBox("The file has been downloaded from the server.", MsgBoxStyle.Information)
            Else
                MsgBox("The file does not exist on the server.", MsgBoxStyle.Exclamation)
            End If
        End Using
    Catch ex As Exception
        MsgBox(ex.ToString, MsgBoxStyle.Critical)
    Finally
        Me.Cursor = Cursors.Default
    End Try
End Sub

My test file took 0.4 seconds to download so it was hard to see the progress. Larger files test really well.

15
  • Are you looking for a progress bar, or to do SFTP with the Progress client? Commented Aug 4, 2015 at 15:39
  • What I would like is a progress report so that I can update a progress bar or a label with the status. Commented Aug 4, 2015 at 15:42
  • It's odd because the codeplex site says "Provide status report for upload and download sftp operations to allow accurate progress bar implementation" but cant really find how this is implemented. Commented Aug 4, 2015 at 15:56
  • There is a Downloading() event on the client - that is where I would look for progress. Downloaded help (.chm file) doesn't seem to work so there doesn't appear to be any usable documentation. Right click the ssfnet client object and goto definition. The event args has Size and Downloaded. Commented Aug 4, 2015 at 19:58
  • OK i see it but thats for the ScpClient class. Im using the SftpClient class. Although I see now that the SftpClient.DownloadFile has an option for a callback. So i could do sftp.DownloadFile(remoteFileName, ms, AddressOf DownloadCallback). Sort of works. Maybe my file is too small to display in a progress. Commented Aug 4, 2015 at 20:35

1 Answer 1

3

I've done some research and found an example in the codeplex discussion forum. Out of that I learned that there is another downloading function which is asynchronous which I will use. Its a good approach to displaying progress in the debug window and also a progressbar control. Feel free to comment

    Imports Renci.SshNet
    Imports System.IO
    Imports Renci.SshNet.Sftp
    Dim fileSize As Long

    Private Sub btnGo_Click(sender As Object, e As EventArgs) Handles btnGo.Click
    Try
        Using sftp As New SftpClient("0.0.0.0", 25, "id", "pwd") 
            'connect to the server
            sftp.Connect()

            'the name of the remote file we want to transfer to the PC
            Dim remoteFileName As String = "/Data/OUT/Config.txt"

            'check for existence of the file
            Dim IsExists As Boolean = sftp.Exists(remoteFileName)
            If IsExists Then
                'get the attributes of the file (namely the size)
                Dim att As Sftp.SftpFileAttributes = sftp.GetAttributes(remoteFileName)
                fileSize = att.Size

                'download the file as a memory stream and convert to a file stream
                Using ms As New MemoryStream
                    'download as memory stream
                    'sftp.DownloadFile(remoteFileName, ms, AddressOf DownloadCallback) 'with download progress
                    'sftp.DownloadFile(remoteFileName, ms) 'without download progress

                    'here we try an asynchronous operation and wait for it to complete.
                    Dim asyncr As IAsyncResult = sftp.BeginDownloadFile(remoteFileName, ms)
                    Dim sftpAsyncr As SftpDownloadAsyncResult = CType(asyncr, SftpDownloadAsyncResult)
                    While Not sftpAsyncr.IsCompleted
                        Dim pct As Integer = CInt((sftpAsyncr.DownloadedBytes / fileSize) * 100)
                        Debug.Print("Downloaded {0} of {1} ({2}%).", sftpAsyncr.DownloadedBytes, fileSize, pct)
                        pgbMain.Value = pct

                        Application.DoEvents()
                    End While
                    sftp.EndDownloadFile(asyncr)

                    'create a file stream
                    Dim localFileName As String = "c:\" & Date.Now.ToString("yyyy-dd-MM_HHmmss") & "_test.txt"
                    Dim fs As New FileStream(localFileName, FileMode.Create, FileAccess.Write)

                    'write the memory stream to the file stream
                    ms.WriteTo(fs)

                    'close file stream
                    fs.Close()

                    'close memory stream
                    ms.Close()
                End Using

                'disconnect from the server
                sftp.Disconnect()

                'success
                MsgBox("The file has been downloaded from the server.", MsgBoxStyle.Information)
            Else
                MsgBox("The file does not exist on the server.", MsgBoxStyle.Exclamation)
            End If
        End Using
    Catch ex As Exception
        MsgBox(ex.ToString, MsgBoxStyle.Critical)
    Finally
        Me.Cursor = Cursors.Default
    End Try
End Sub
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.