Rebex FTP/SSL

FTP and FTP/SSL client .NET library

Download 30-day free trial Buy from $349

Easy-to-use API

Transferring files 

A typical FTP session goes through the following steps:

The following code uploads a file to a server and downloads another one:

// create FTP client instance
using (var ftp = new Rebex.Net.Ftp())
{
    // connect to a server
    ftp.Connect(hostname);

    // authenticate
    ftp.Login(username, password);

    // upload a file
    ftp.PutFile(@"C:\MyData\file1.txt", "/MyData/file1.txt");

    // download a file
    ftp.GetFile("/MyData/file2.txt", @"C:\MyData\file2.txt");

    // disconnect (not required, but polite)
    ftp.Disconnect();
}
' create FTP client instance
Using ftp As New Rebex.Net.Ftp
    ' connect to a server
    ftp.Connect(hostname)

    ' authenticate
    ftp.Login(username, password)

    ' upload a file
    ftp.PutFile("C:\MyData\file1.txt", "/MyData/file1.txt")

    ' download a file
    ftp.GetFile("/MyData/file2.txt", "C:\MyData\file2.txt")

    ' disconnect (not required, but polite)
    ftp.Disconnect()
End Using
For more information about these file transfer methods, check out Multiple files operations.

Transferring directories 

Transferring directories is just as simple as transferring files. Just use a wildcard to match all files and subdirectories in the specified source directory:

// create FTP client instance
using (var ftp = new Rebex.Net.Ftp())
{
    // connect to a server
    ftp.Connect(hostname);

    // authenticate
    ftp.Login(username, password);

    // upload a directory
    ftp.Upload(@"C:\MyData\Web\*", "/MyData/Web");

    // download a directory
    ftp.Download("/MyData/Exports/*", @"C:\MyData\Exports");

    // disconnect (not required, but polite)
    ftp.Disconnect();
}
' create FTP client instance
Using ftp As New Rebex.Net.Ftp
    ' connect to a server
    ftp.Connect(hostname)

    ' authenticate
    ftp.Login(username, password)

    ' upload a directory
    ftp.Upload("C:\MyData\Web\*", "/MyData/Web")

    ' download a directory
    ftp.Download("/MyData/Exports/*", "C:\MyData\Exports")

    ' disconnect (not required, but polite)
    ftp.Disconnect()
End Using
For more information about wildcards, check out Using wildcards.

Transferring files with a specific extension 

To transfer only specific files, use a mask (wildcard pattern).

// create FTP client instance, connect, log in
// ...

// upload all ".txt" files from "MyData" directory only
// (files from subdirectories are not transferred)
ftp.Upload(@"C:\MyData\*.txt", "/MyData", TraversalMode.MatchFilesShallow);

// upload all ".html" files under "MyData" directory anywhere
// (files from subdirectories are transferred as well)
ftp.Upload(@"C:\MyData\*.html", "/MyData", TraversalMode.MatchFilesDeep);
' create client instance, connect, log in
' ...

' upload all ".txt" files from "MyData" directory only
' (files from subdirectories are not transferred)
ftp.Upload("C:\MyData\*.txt", "/MyData", TraversalMode.MatchFilesShallow)

' upload all ".html" files under "MyData" directory anywhere
' (files from subdirectories are transferred as well)
ftp.Upload("C:\MyData\*.html", "/MyData", TraversalMode.MatchFilesDeep)

If wildcards are not powerful enough for you, use the FileSet object instead.

Displaying progress bar 

To display current transfer's progress indicator, write a TransferProgressChanged event handler. Its SftpTransferProgressChangedEventArgs argument has lots of useful properties.

void FtpTransferProgressChanged(object sender, FtpTransferProgressChangedEventArgs e)
{
    // display some useful info about progress
    totalProgressBar.Value = (int)e.ProgressPercentage;
    fileProgressBar.Value = (int)e.CurrentFileProgressPercentage;
    transferSpeedLabel.Text = e.BytesPerSecond.ToString();
}
Sub FtpTransferProgressChanged(ByVal sender As Object, ByVal e As TransferProgressChangedEventArgs)
    ' display some useful info about progress
    totalProgressBar.Value = e.ProgressPercentage
    fileProgressBar.Value = e.CurrentFileProgressPercentage
    transferSpeedLabel.Text = e.BytesPerSecond.ToString()
End Sub

Registering the event handler:

// create FTP client instance, connect, log in
// ...

// register progress event handler
ftp.TransferProgressChanged += FtpTransferProgressChanged;

// transfer files
// ...
' create FTP client instance, connect, log in
' ...

' register progress event handler
AddHandler ftp.TransferProgressChanged, AddressOf FtpTransferProgressChanged

' transfer files
' ...
To learn more about the TransferProgressChanged event, check out Progress reporting.