Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net SFTP与SSH.NET的合作进展_Vb.net_Ssh_Sftp_Progress - Fatal编程技术网

Vb.net SFTP与SSH.NET的合作进展

Vb.net SFTP与SSH.NET的合作进展,vb.net,ssh,sftp,progress,Vb.net,Ssh,Sftp,Progress,VB2010与 我已经下载并实现了这个库来进行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()

VB2010与

我已经下载并实现了这个库来进行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
编辑:好的,我做了一些研究,在codeplex讨论论坛上找到了一个例子。从中我了解到还有另一个异步下载功能,我将使用它。这是在调试窗口和progressbar控件中显示进度的好方法。请随意评论

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

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

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

    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

您是在寻找进度条,还是要与进度客户端进行SFTP?我想要的是一个进度报告,以便我可以用状态更新进度条或标签。这很奇怪,因为codeplex网站上说“为上传和下载SFTP操作提供状态报告,以允许准确的进度条实施”但是我真的找不到这是如何实现的。客户端上有一个Downloading()事件,我会在那里查找进度。下载的帮助(.chm文件)似乎不起作用,因此似乎没有任何可用的文档。右键单击ssfnet客户端对象并转到定义。事件参数有大小和已下载。好的,我看到了,但这是针对ScpClient类的。我正在使用SftpClient类。尽管我现在看到SftpClient.DownloadFile有一个回调选项。所以我可以做sftp.DownloadFile(remoteFileName,ms,AddressOf DownloadCallback)。这类作品。可能我的文件太小,无法在进程中显示。