Differences
This shows you the differences between the selected revisions of the page.
| library_session_filetransferprogress 2013-05-23 | library_session_filetransferprogress 2024-09-12 (current) | ||
| Line 1: | Line 1: | ||
| ====== Session.FileTransferProgress Event ====== | ====== Session.FileTransferProgress Event ====== | ||
| - | &beta_feature | ||
| - | Occurs during file tranfer to report transfer progress. Occurs as part of either ''[[library_session_getfiles|Session.GetFiles]]'', ''[[library_session_putfiles|Session.PutFiles]]'' or ''[[library_session_synchronizedirectories|Session.SynchronizeDirectories]]''. | + | Occurs during file transfer to report transfer progress. Occurs as part of either ''[[library_session_getfiles|Session.GetFiles]]'', ''[[library_session_putfiles|Session.PutFiles]]'' (and other derived download and upload methods) or ''[[library_session_synchronizedirectories|Session.SynchronizeDirectories]]''. |
| ===== Syntax ===== | ===== Syntax ===== | ||
| <code csharp *> | <code csharp *> | ||
| - | public delegate void FileTransferProgressEventHandler(object sender, FileTransferProgressEventArgs e); | + | public delegate void FileTransferProgressEventHandler( |
| + | ····object sender, | ||
| + | ···FileTransferProgressEventArgs e | ||
| + | ); | ||
| public event FileTransferredEventHandler FileTransferred; | public event FileTransferredEventHandler FileTransferred; | ||
| </code> | </code> | ||
| Line 12: | Line 14: | ||
| <code vbnet *> | <code vbnet *> | ||
| Public Delegate Sub FileTransferProgressEventHandler( | Public Delegate Sub FileTransferProgressEventHandler( | ||
| - | ByVal sender As Object, ByVal e As FileTransferProgressEventArgs) | + | sender As Object, |
| + | ···e As FileTransferProgressEventArgs | ||
| + | ) | ||
| Public Custom Event FileTransferred As FileTransferredEventHandler | Public Custom Event FileTransferred As FileTransferredEventHandler | ||
| </code> | </code> | ||
| Line 20: | Line 24: | ||
| The event is raised, when starting transfer of every file, and then at most once a second for duration of the transfer. | The event is raised, when starting transfer of every file, and then at most once a second for duration of the transfer. | ||
| + | |||
| + | The event has to be subscribed before calling ''[[library_session_open|Open]]''. | ||
| See also ''[[library_session_filetransferred|Session.FileTransferred]]''. | See also ''[[library_session_filetransferred|Session.FileTransferred]]''. | ||
| ===== [[example]] Examples ===== | ===== [[example]] Examples ===== | ||
| + | |||
| ==== [[csharp]] C# Example ==== | ==== [[csharp]] C# Example ==== | ||
| <code csharp> | <code csharp> | ||
| Line 36: | Line 43: | ||
| { | { | ||
| // Setup session options | // Setup session options | ||
| - | SessionOptions sessionOptions = new SessionOptions { | + | SessionOptions sessionOptions = new SessionOptions |
| + | ···········{ | ||
| Protocol = Protocol.Sftp, | Protocol = Protocol.Sftp, | ||
| HostName = "example.com", | HostName = "example.com", | ||
| UserName = "user", | UserName = "user", | ||
| Password = "mypassword", | Password = "mypassword", | ||
| - | SshHostKeyFingerprint = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" | + | SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..." |
| }; | }; | ||
| Line 55: | Line 63: | ||
| { | { | ||
| // Download files and throw on any error | // Download files and throw on any error | ||
| - | session.GetFiles("/home/martin/public_html/", "d:\\backup\\").Check(); | + | session.GetFiles("/home/martin/public_html/*", @"d:\backup\").Check(); |
| } | } | ||
| finally | finally | ||
| Line 76: | Line 84: | ||
| } | } | ||
| - | private static void SessionFileTransferProgress(object sender, FileTransferProgressEventArgs e) | + | private static void SessionFileTransferProgress( |
| + | ········object sender, FileTransferProgressEventArgs e) | ||
| { | { | ||
| // New line for every new file | // New line for every new file | ||
| Line 97: | Line 106: | ||
| ==== [[vbnet]] VB.NET Example ==== | ==== [[vbnet]] VB.NET Example ==== | ||
| <code vbnet> | <code vbnet> | ||
| - | Imports System | ||
| Imports WinSCP | Imports WinSCP | ||
| Line 112: | Line 120: | ||
| .UserName = "user" | .UserName = "user" | ||
| .Password = "mypassword" | .Password = "mypassword" | ||
| - | .SshHostKeyFingerprint = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" | + | .SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..." |
| End With | End With | ||
| - | Using session As Session = New Session | + | Using session As New Session |
| ' Will continuously report progress of transfer | ' Will continuously report progress of transfer | ||
| AddHandler session.FileTransferProgress, AddressOf SessionFileTransferProgress | AddHandler session.FileTransferProgress, AddressOf SessionFileTransferProgress | ||
| Line 125: | Line 133: | ||
| ' Download files and throw on any error | ' Download files and throw on any error | ||
| session.GetFiles( _ | session.GetFiles( _ | ||
| - | "/home/martin/public_html/", "d:\backup\").Check | + | "/home/martin/public_html/*", "d:\backup\").Check() |
| Finally | Finally | ||
| ' Terminate line after the last file (if any) | ' Terminate line after the last file (if any) | ||
| - | If Example._lastFileName IsNot Nothing Then | + | If _lastFileName IsNot Nothing Then |
| - | Console.WriteLine | + | Console.WriteLine() |
| End If | End If | ||
| End Try | End Try | ||
| Line 143: | Line 151: | ||
| Private Shared Sub SessionFileTransferProgress( | Private Shared Sub SessionFileTransferProgress( | ||
| - | ByVal sender As Object, ByVal e As FileTransferProgressEventArgs) | + | sender As Object, e As FileTransferProgressEventArgs) |
| ' New line for every new file | ' New line for every new file | ||
| - | If (_lastFileName IsNot Nothing) AndAlso (_lastFileName <> e.FileName)) Then | + | If (_lastFileName IsNot Nothing) AndAlso (_lastFileName <> e.FileName) Then |
| - | Console.WriteLine | + | Console.WriteLine() |
| End If | End If | ||
| Line 163: | Line 171: | ||
| Learn more about [[library_powershell|using WinSCP .NET assembly from PowerShell]]. | Learn more about [[library_powershell|using WinSCP .NET assembly from PowerShell]]. | ||
| - | &beta (* .dll *) | ||
| <code powershell> | <code powershell> | ||
| # Load WinSCP .NET assembly | # Load WinSCP .NET assembly | ||
| - | # Use "winscp.dll" for the releases before the latest beta version. | + | Add-Type -Path "WinSCPnet.dll" |
| - | [Reflection.Assembly]::LoadFrom("WinSCPnet.dll") | Out-Null | + | |
| # Session.FileTransferProgress event handler | # Session.FileTransferProgress event handler | ||
| Line 173: | Line 179: | ||
| function FileTransferProgress | function FileTransferProgress | ||
| { | { | ||
| - | Param($e) | + | param($e) |
| # New line for every new file | # New line for every new file | ||
| Line 195: | Line 201: | ||
| try | try | ||
| { | { | ||
| - | $sessionOptions = New-Object WinSCP.SessionOptions | + | $sessionOptions = New-Object WinSCP.SessionOptions -Property @{ |
| - | ···$sessionOptions.Protocol = [WinSCP.Protocol]::Sftp | + | ········Protocol = [WinSCP.Protocol]::Sftp |
| - | $sessionOptions.HostName = "example.com" | + | ·······HostName = "example.com" |
| - | $sessionOptions.UserName = "user" | + | ·······UserName = "user" |
| - | $sessionOptions.Password = "mypassword" | + | ·······Password = "mypassword" |
| - | $sessionOptions.SshHostKeyFingerprint = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" | + | ·······SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..." |
| + | } | ||
| $session = New-Object WinSCP.Session | $session = New-Object WinSCP.Session | ||
| Line 212: | Line 219: | ||
| # Download the file and throw on any error | # Download the file and throw on any error | ||
| - | $session.GetFiles("/home/martin/public_html/", "d:\backup\").Check() | + | $session.GetFiles("/home/martin/public_html/*", "d:\backup\").Check() |
| } | } | ||
| finally | finally | ||
| Line 228: | Line 235: | ||
| exit 0 | exit 0 | ||
| } | } | ||
| - | catch [Exception] | + | catch |
| { | { | ||
| - | Write-Host $_.Exception.Message | + | Write-Host "Error: $($_.Exception.Message)" |
| exit 1 | exit 1 | ||
| } | } | ||
| </code> | </code> | ||
| + | |||
| + | If you want a pseudo-GUI progress bar, you can make use of ''[[ps>microsoft.powershell.utility/write-progress|Write-Progress]]'' cmdlet: | ||
| + | |||
| + | <code powershell> | ||
| + | function FileTransferProgress | ||
| + | { | ||
| + | param($e) | ||
| + | |||
| + | Write-Progress ` | ||
| + | -Activity "Uploading" -Status ("{0:P0} complete:" -f $e.OverallProgress) ` | ||
| + | -PercentComplete ($e.OverallProgress * 100) | ||
| + | Write-Progress ` | ||
| + | -Id 1 -Activity $e.FileName -Status ("{0:P0} complete:" -f $e.FileProgress) ` | ||
| + | -PercentComplete ($e.FileProgress * 100) | ||
| + | } | ||
| + | </code> | ||
| + | |||
| + | ==== Real-Life Example ==== | ||
| + | |||
| + | * [[library_example_winforms_progressbar|*]]. | ||
| + | |||