PowerTCP FTP for .NET
Progress Event
Example 



Reports progress information at the frequency specified by FtpSession.ProgressSize.
Syntax
Public Event Progress As EventHandler(Of ProgressEventArgs)
Dim instance As Ftp
Dim handler As EventHandler(Of ProgressEventArgs)
 
AddHandler instance.Progress, handler
public event EventHandler<ProgressEventArgs> Progress
public:
event EventHandler<ProgressEventArgs^>^ Progress
Event Data

The event handler receives an argument of type ProgressEventArgs containing data related to this event. The following ProgressEventArgs properties provide information specific to this event.

PropertyDescription
Item Gets progress for the current file or directory in the copy operation.  
List Gets progress for the entire copy operation.  
Remarks

See the SynchronizingObject property for information on updating UI controls from within your event handler.

Example
This example demonstrates using the FTP control to retrieve a file using the FTP GET command.
private void button1_Click(object sender, EventArgs e)
{
    //Wire up the FTP component's progress event to track the transfer's progress
    ftp1.Progress += new EventHandler<ProgressEventArgs>(ftp1_Progress);

    //Wire up the FTP component's error event to report errors
    ftp1.Error += new EventHandler<ErrorEventArgs>(ftp1_Error);

    //Setup the Ftp session and then connect, authenticate, and retrieve a file on a worker thread
    ftp1.Session.RemoteEndPoint.HostNameOrAddress = myServer;
    ftp1.Session.Username = myUsername;
    ftp1.Session.Password = myPassword;

    //The Start method executes the GET operation on a worker thread so the UI is not blocked.
    //Unhandled worker thread exceptions are automatically caught and marshaled to the Error event
    ftp1.Start(getFile, null);
}

private void getFile(object state)
{
    try
    {
        //Connect to the FTP Server.
        ftp1.Connect();

        //Authenticate the user.
        ftp1.Authenticate();

        //Retrieve a file.
        ftp1.Get("testfile.txt", myLocalDirectory + "\\testfile.txt", Synchronize.Off);
    }
    catch (Exception Ex)
    {
        //If an exception occurs, marshal it to the UI thread.
        ftp1.Marshal(Ex);
    }
    finally
    {
        //Close the connection.
        ftp1.Close();
    }
}

void ftp1_Progress(object sender, ProgressEventArgs e)
{
    //Transfer progress can be provided via this event, for a more complete
    //example of reporting progress, please see the Ftp Listview sample.

    //The file transfer is in progress.
    if (e.Item.Status == CopyStatus.InProgress)
    {
        if (e.Item.Count > 0)
        {
            //Update the progress of the transfer.
            progressBar1.Value = e.Item.Percentage;
        }
    }
    //The file transfer is complete.
    else if (e.Item.Status == CopyStatus.Completed)
    {
        //Reset the progressbar
        progressBar1.Value = 0;
    }
    //The file transfer was aborted or failed.
    else if (e.Item.Status == CopyStatus.Aborted || e.Item.Status == CopyStatus.Failed)
    {
        //Reset the progress bar
        progressBar1.Value = 0;

        //Report that the transfer has stopped.
        MessageBox.Show("The file transfer has failed or was aborted.", "The File transfer has stopped.", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

void ftp1_Error(object sender, ErrorEventArgs e)
{
    //Show a messagebox containg the exception.
    MessageBox.Show(e.GetException().ToString(), "An error has occurred.", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
    'Wire up the FTP component's progress event to track the transfer's progress
    AddHandler ftp1.Progress, AddressOf ftp1_Progress

    'Wire up the FTP component's error event to report errors
    AddHandler ftp1.Error, AddressOf ftp1_Error

    'Setup the Ftp session and then connect, authenticate, and retrieve a file on a worker thread
    ftp1.Session.RemoteEndPoint.HostNameOrAddress = myServer
    ftp1.Session.Username = myUsername
    ftp1.Session.Password = myPassword

    'The Start method executes the GET operation on a worker thread so the UI is not blocked.
    'Unhandled worker thread exceptions are automatically caught and marshaled to the Error event
    ftp1.Start(AddressOf getFile, Nothing)
End Sub

Private Sub getFile(ByVal state As Object)
    Try
        'Connect to the FTP Server.
        ftp1.Connect()

        'Authenticate the user.
        ftp1.Authenticate()

        'Retrieve a file.
        ftp1.Get("testfile.txt", myLocalDirectory & "\testfile.txt", Synchronize.Off)
    Catch Ex As Exception
        'If an exception occurs, marshal it to the UI thread.
        ftp1.Marshal(Ex)
    Finally
        'Close the connection.
        ftp1.Close()
    End Try
End Sub

Private Sub ftp1_Progress(ByVal sender As Object, ByVal e As ProgressEventArgs)
    'Transfer progress can be provided via this event, for a more complete
    'example of reporting progress, please see the Ftp Listview sample.

    'The file transfer is in progress.
    If e.Item.Status = CopyStatus.InProgress Then
        If e.Item.Count > 0 Then
            'Update the progress of the transfer.
            progressBar1.Value = e.Item.Percentage
        End If
    'The file transfer is complete.
    ElseIf e.Item.Status = CopyStatus.Completed Then
        'Reset the progressbar
        progressBar1.Value = 0
    'The file transfer was aborted or failed.
    ElseIf e.Item.Status = CopyStatus.Aborted OrElse e.Item.Status = CopyStatus.Failed Then
        'Reset the progress bar
        progressBar1.Value = 0

        'Report that the transfer has stopped.
        MessageBox.Show("The file transfer has failed or was aborted.", "The File transfer has stopped.", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End If
End Sub

Private Sub ftp1_Error(ByVal sender As Object, ByVal e As ErrorEventArgs)
    'Show a messagebox containg the exception.
    MessageBox.Show(e.GetException().ToString(), "An error has occurred.", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Sub
See Also

Reference

Ftp Class
Ftp Members


PowerTCP FTP for .NET Documentation Version 6.1
© 2023 Dart Communications. All Rights Reserved.
Send comments on this topic