More .NET libraries
-
Rebex FTP/SSL
.NET FTP client
-
Rebex SSH Shell
.NET SSH Shell
-
Rebex Total Pack
All Rebex .NET libraries together
Back to feature list...
Easy-to-use API
On this page:
Transferring files
A typical SFTP session goes through the following steps:
- Connect to an SFTP server
- Verify the server fingerprint using Fingerprint property or FingerprintCheck event handler
- Login - authenticate with a user name and password or another authentication method
- Browse directories, transfer files, etc.
- Disconnect
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...