• Loading...
ComponentPro UltimateSftp

Control file transfer process

Language Filter: AllSend comments on this topic to ComponentPro

Upload

The TransferConfirm and Progress events of the Sftp class give you the ease of controlling files to upload and showing progress while transferring data. The following example illustrates how to register event handlers to the TransferConfirm and Progress events to monitor the file upload process.

using System;
using ComponentPro.IO;
using ComponentPro.Net;

...

public void DoUpload()
{
    // Create a new class instance. 
    Sftp client = new Sftp();

    // Connect to the SFTP server. 
    client.Connect("demo.componentpro.com");

    // Authenticate. 
    client.Authenticate("test", "test");

    // ... 
 
    client.TransferConfirm += client_TransferConfirm;
    client.Progress += client_Progress;

    try 
    {
        TransferOptions opt = new TransferOptions("*.*", FileOverwriteMode.Confirm);

        // Get all directories, subdirectories, and files in local folder 'c:\myfolder' to remote folder '/myfolder'. 
        client.Upload("c:\\myfolder", "/myfolder", opt);
    }
    catch (Exception exc)
    {
        Console.WriteLine("Error: " + exc.Message);
    }

    // ... 
 
    // Disconnect. 
    client.Disconnect();
}

void client_Progress(object sender, ComponentPro.IO.FileSystemProgressEventArgs e)
{
    // Show progress info. 
    Console.WriteLine("Current File: %{0} completed", e.Percentage);
    Console.WriteLine("Total: %{0} completed", e.TotalPercentage);

    switch (e.State)
    {
        case TransferState.StartUploadingFile:
            if (e.SourcePath.EndsWith(".exe"))
            {
                // Skip all .exe files 
                e.Skip = true;  
            }
            else if (e.SourcePath.StartsWith(@"C:\MyFolder"))
            {
                // Change the source file path if it starts with "C:\MyFolder" 
                e.SourcePath = e.SourcePath.Replace(@"C:\MyFolder", @"C:\MySecondFolder");
            }
            break;
    }
}

void client_TransferConfirm(object sender, ComponentPro.IO.TransferConfirmEventArgs e)
{
    if (e.Exception != null)
        Console.WriteLine("Error: " + e.Exception.Message);

    if (e.ConfirmReason == TransferConfirmReason.FileAlreadyExists)
    {
        // Skip the existing file. 
        e.NextAction = TransferConfirmNextActions.Skip;
    }

    Console.Write("Do you want to (r)etry or (s)kip?");
    string key = Console.ReadLine();
    if (key == "r")
        e.NextAction = TransferConfirmNextActions.Retry;
    else if (key == "s")
        e.NextAction = TransferConfirmNextActions.Skip;
    else 
        // Cancel the operation. 
        e.NextAction = TransferConfirmNextActions.Cancel;
}

Download

Similarly we have the following example for the Download method:

using System;
using ComponentPro.IO;
using ComponentPro.Net;

...

public void DoDownload()
{
    // Create a new class instance. 
    Sftp client = new Sftp();

    // Connect to the SFTP server. 
    client.Connect("demo.componentpro.com");

    // Authenticate. 
    client.Authenticate("test", "test");

    // ... 
 
    client.TransferConfirm += client_TransferConfirm;
    client.Progress += client_Progress;

    try 
    {
        TransferOptions opt = new TransferOptions("*.*", FileOverwriteMode.Confirm);

        // Get all directories, subdirectories, and files in remote folder '/myfolder' to 'c:\myfolder'. 
        client.Download("/myfolder", "c:\\myfolder", opt);
    }
    catch (Exception exc)
    {
        Console.WriteLine("Error: " + exc.Message);
    }

    // ... 
 
    // Disconnect. 
    client.Disconnect();
}

void client_Progress(object sender, ComponentPro.IO.FileSystemProgressEventArgs e)
{
    // Show progress info. 
    Console.WriteLine("Current File: %{0} completed", e.Percentage);
    Console.WriteLine("Total: %{0} completed", e.TotalPercentage);

    switch (e.State)
    {
        case TransferState.StartDownloadingFile:
            if (e.SourcePath.EndsWith(".exe"))
            {
                // Skip all .exe files 
                e.Skip = true;
            }
            else if (e.SourcePath.StartsWith("/MyFolder"))
            {
                // Change the source file path if it starts with "/MyFolder" 
                e.SourcePath = e.SourcePath.Replace("/MyFolder", "/MySecondFolder");
            }
            break;
    }
}

void client_TransferConfirm(object sender, ComponentPro.IO.TransferConfirmEventArgs e)
{
    if (e.Exception != null)
        Console.WriteLine("Error: " + e.Exception.Message);

    if (e.ConfirmReason == TransferConfirmReason.FileAlreadyExists)
    {
        // Skip the existing file. 
        e.NextAction = TransferConfirmNextActions.Skip;
    }

    Console.Write("Do you want to (r)etry or (s)kip?");
    string key = Console.ReadLine();
    if (key == "r")
        e.NextAction = TransferConfirmNextActions.Retry;
    else if (key == "s")
        e.NextAction = TransferConfirmNextActions.Skip;
    else 
        // Cancel the operation. 
        e.NextAction = TransferConfirmNextActions.Cancel;
}