More .NET libraries
-
Rebex SFTP
.NET SFTP client
-
Rebex File Transfer Pack
FTP and SFTP together
-
Rebex Total Pack
All Rebex .NET libraries together
Back to feature list...
Easy-to-use API
On this page:
Transferring files
A typical FTP session goes through the following steps:
- Connect to an FTP server
- Validate the server certificate (Rebex FTP does it automatically by default, but you can do the validation yourself)
- Login - authenticate with a user name and password or using a certificate
- Browse directories, transfer files, etc.
- Disconnect
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
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
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
' ...
TransferProgressChanged
event, check out
Progress reporting.
Back to feature list...