Rebex SFTP

SFTP and SCP client .NET library

Download 30-day free trial Buy from $349
More .NET libraries

Back to feature list...

Easy-to-use API

Transferring files 

A typical SFTP session goes through the following steps:

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

// create SFTP client instance
using (var sftp = new Rebex.Net.Sftp())
{
    // connect to a server
    sftp.Connect(hostname);

    // verify server's fingerprint
    // (see Security section for details)
    // ...

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

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

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

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

    ' verify server's fingerprint
    ' (see Security section for details)
    ' ...

    ' authenticate
    sftp.Login(username, password)

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

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

    ' disconnect (not required, but polite)
    sftp.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 SFTP client instance
using (var sftp = new Rebex.Net.Sftp())
{
    // connect to a server
    sftp.Connect(hostname);

    // verify server's fingerprint
    // (see Security section for details)
    // ...

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

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

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

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

    ' verify server's fingerprint
    ' (see Security section for details)
    ' ...

    ' authenticate
    sftp.Login(username, password)

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

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

    ' disconnect (not required, but polite)
    sftp.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 SFTP client instance, connect, verify server's fingerprint, log in
// ...

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

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

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

' upload all ".html" files under "MyData" directory anywhere
' (files from subdirectories are transferred as well)
sftp.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 SftpTransferProgressChanged(object sender, SftpTransferProgressChangedEventArgs e)
{
    // display some useful info about progress
    totalProgressBar.Value = (int)e.ProgressPercentage;
    fileProgressBar.Value = (int)e.CurrentFileProgressPercentage;
    transferSpeedLabel.Text = e.BytesPerSecond.ToString();
}
Sub SftpTransferProgressChanged(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 SFTP client instance, connect, verify server's fingerprint, log in
// ...

// register progress event handler
sftp.TransferProgressChanged += SftpTransferProgressChanged;

// transfer files
// ...
' create SFTP client instance, connect, verify server's fingerprint, log in
' ...

' register progress event handler
AddHandler sftp.TransferProgressChanged, AddressOf SftpTransferProgressChanged

' transfer files
' ...

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

Back to feature list...