vb.net SSH.NET 的 SFTP 进度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31812582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:22:16  来源:igfitidea点击:

SFTP Progress with SSH.NET

vb.netsshsftpprogress

提问by sinDizzy

VB2010 with SSH.NET.

带有SSH.NET 的 VB2010

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:

我已经下载并实现了该库来进行 SFTP 下载,并且效果很好。我一直在查看文档和示例,但看不到如何逐步实现 SFTP 下载。我想显示下载的进度。到目前为止,我有:

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.

编辑:好的,我做了一些研究,并在 codeplex 讨论论坛中找到了一个例子。从中我了解到还有另一个异步下载功能,我将使用它。它是在调试窗口中显示进度以及进度条控件的好方法。随意发表评论。

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.

我的测试文件下载耗时 0.4 秒,因此很难看到进度。较大的文件测试非常好。

采纳答案by sinDizzy

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

我做了一些研究,并在 codeplex 讨论论坛中找到了一个例子。从中我了解到还有另一个异步下载功能,我将使用它。它是在调试窗口中显示进度以及进度条控件的好方法。随意评论

    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