VB2010 con SSH.NET https://sshnet.codeplex.com/ Ho scaricato e implementato la libreria per eseguire un download SFTP e funziona benissimo. Ho esaminato la documentazione e gli esempi e non vedo come implementare un download SFTP con il progresso. Voglio visualizzare l'avanzamento del download mentre si verifica. Finora ho:Progresso SFTP con SSH.NET
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
Modifica: ok Ho fatto qualche ricerca e ho trovato un esempio nel forum di discussione di codeplex. Ho imparato che c'è un'altra funzione di download che è asincrona che userò. È un buon approccio per visualizzare i progressi nella finestra di debug e anche un controllo progressbar. Sentiti libero di commentare.
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
Il file di prova ha impiegato 0.4 secondi per il download, quindi è stato difficile vedere i progressi. I file più grandi testano davvero bene.
Stai cercando una barra di avanzamento, o per eseguire SFTP con il client Progress? –
Quello che vorrei è un rapporto sullo stato di avanzamento in modo che possa aggiornare una barra di avanzamento o un'etichetta con lo stato. – sinDizzy
È strano perché il sito di codeplex dice "Fornisci un rapporto sullo stato per caricare e scaricare le operazioni sftp per consentire un'implementazione accurata della barra di avanzamento", ma non riesco davvero a trovare il modo in cui viene implementato. – sinDizzy