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 =====
 +
 +==== [[csharp]] C# Example ====
 +<code csharp>
 +using System;
 +using WinSCP;
 +
 +class Example
 +{
 +    public static int Main()
 +    {
 +        try
 +        {
 +            // Setup session options
 +            SessionOptions sessionOptions = new SessionOptions
 +            {
 +                Protocol = Protocol.Sftp,
 +                HostName = "example.com",
 +                UserName = "user",
 +                Password = "mypassword",
 +                SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..."
 +            };
 +
 +            using (Session session = new Session())
 +            {
 +                // Will continuously report progress of transfer
 +                session.FileTransferProgress += SessionFileTransferProgress;
 +
 +                // Connect
 +                session.Open(sessionOptions);
 +
 +                try
 +                {
 +                    // Download files and throw on any error
 +                    session.GetFiles("/home/martin/public_html/*", @"d:\backup\").Check();
 +                }
 +                finally
 +                {
 +                    // Terminate line after the last file (if any)
 +                    if (_lastFileName != null)
 +                    {
 +                        Console.WriteLine();
 +                    }
 +                }
 +            }
 +
 +            return 0;
 +        }
 +        catch (Exception e)
 +        {
 +            Console.WriteLine("Error: {0}", e);
 +            return 1;
 +        }
 +    }
 +
 +    private static void SessionFileTransferProgress(
 +        object sender, FileTransferProgressEventArgs e)
 +    {
 +        // New line for every new file
 +        if ((_lastFileName != null) && (_lastFileName != e.FileName))
 +        {
 +            Console.WriteLine();
 +        }
 +
 +        // Print transfer progress
 +        Console.Write("\r{0} ({1:P0})", e.FileName, e.FileProgress);
 +
 +        // Remember a name of the last file reported
 +        _lastFileName = e.FileName;
 +    }
 +
 +    private static string _lastFileName;
 +}
 +</code>
 +
 +==== [[vbnet]] VB.NET Example ====
 +<code vbnet>
 +Imports WinSCP
 +
 +Friend Class Example
 +
 +    Public Shared Function Main() As Integer
 +
 +        Try
 +            ' Setup session options
 +            Dim sessionOptions As New SessionOptions
 +            With sessionOptions
 +                .Protocol = Protocol.Sftp
 +                .HostName = "example.com"
 +                .UserName = "user"
 +                .Password = "mypassword"
 +                .SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..."
 +            End With
 +
 +            Using session As New Session
 +                ' Will continuously report progress of transfer
 +                AddHandler session.FileTransferProgress, AddressOf SessionFileTransferProgress
 +
 +                ' Connect
 +                session.Open(sessionOptions)
 +
 +                Try
 +                    ' Download files and throw on any error
 +                    session.GetFiles( _
 +                        "/home/martin/public_html/*", "d:\backup\").Check()
 +                Finally
 +                    ' Terminate line after the last file (if any)
 +                    If _lastFileName IsNot Nothing Then
 +                        Console.WriteLine()
 +                    End If
 +                End Try
 +            End Using
 +
 +            Return 0
 +        Catch e As Exception
 +            Console.WriteLine("Error: {0}", e)
 +            Return 1
 +        End Try
 +
 +    End Function
 +
 +    Private Shared Sub SessionFileTransferProgress(
 +            sender As Object, e As FileTransferProgressEventArgs)
 +        ' New line for every new file
 +        If (_lastFileName IsNot Nothing) AndAlso (_lastFileName <> e.FileName) Then
 +            Console.WriteLine()
 +        End If
 +
 +        ' Print transfer progress
 +        Console.Write("\r{0} ({1:P0})", e.FileName, e.FileProgress)
 +
 +        ' Remember a name of the last file reported
 +        _lastFileName = e.FileName
 +    End Sub
 +
 +    Private Shared _lastFileName As String
 +End Class
 +</code>
 +
 +==== [[powershell]] PowerShell Example ====
 +Learn more about [[library_powershell|using WinSCP .NET assembly from PowerShell]].
 +
 +<code powershell>
 +# Load WinSCP .NET assembly
 +Add-Type -Path "WinSCPnet.dll"
 +
 +# Session.FileTransferProgress event handler
 +
 +function FileTransferProgress
 +{
 +    param($e)
 +
 +    # New line for every new file
 +    if (($script:lastFileName -ne $Null) -and
 +        ($script:lastFileName -ne $e.FileName))
 +    {
 +        Write-Host
 +    }
 +
 +    # Print transfer progress
 +    Write-Host -NoNewline ("`r{0} ({1:P0})" -f $e.FileName, $e.FileProgress)
 +
 +    # Remember a name of the last file reported
 +    $script:lastFileName = $e.FileName
 +}
 +
 +# Main script
 +
 +$script:lastFileName = $Null
 +
 +try
 +{
 +    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
 +        Protocol = [WinSCP.Protocol]::Sftp
 +        HostName = "example.com"
 +        UserName = "user"
 +        Password = "mypassword"
 +        SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..."
 +    }
 +
 +    $session = New-Object WinSCP.Session
 +    try
 +    {
 +        # Will continuously report progress of transfer
 +        $session.add_FileTransferProgress( { FileTransferProgress($_) } )
 +
 +        # Connect
 +        $session.Open($sessionOptions)
 +
 +        # Download the file and throw on any error
 +        $session.GetFiles("/home/martin/public_html/*", "d:\backup\").Check()
 +    }
 +    finally
 +    {
 +        # Terminate line after the last file (if any)
 +        if ($script:lastFileName -ne $Null)
 +        {
 +            Write-Host
 +        }
 +
 +        # Disconnect, clean up
 +        $session.Dispose()
 +    }
 +
 +    exit 0
 +}
 +catch
 +{
 +    Write-Host "Error: $($_.Exception.Message)"
 +    exit 1
 +}
 +</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|*]].

Last modified: by martin