-
-
Notifications
You must be signed in to change notification settings - Fork 657
Quick Start Example
Robin Rodricks edited this page Jul 23, 2022
·
14 revisions
This code demonstrates the simplest possible way to connect to an FTP server and perform FTP operations. See below for the same example using async/await operators.
// create an FTP client and specify the host, username and password
// (delete the credentials to use the "anonymous" account)
FtpClient client = new FtpClient("123.123.123.123", "david", "pass123");
// connect to the server and automatically detect working FTP settings
client.AutoConnect();
// get a list of files and directories in the "/htdocs" folder
foreach (FtpListItem item in client.GetListing("/htdocs")) {
// if this is a file
if (item.Type == FtpFileSystemObjectType.File){
// get the file size
long size = client.GetFileSize(item.FullName);
// calculate a hash for the file on the server side (default algorithm)
FtpHash hash = client.GetChecksum(item.FullName);
}
// get modified date/time of the file or folder
DateTime time = client.GetModifiedTime(item.FullName);
}
// upload a file
client.UploadFile(@"C:\MyVideo.mp4", "/htdocs/MyVideo.mp4");
// move the uploaded file
client.MoveFile("/htdocs/MyVideo.mp4", "/htdocs/MyVideo_2.mp4");
// download the file again
client.DownloadFile(@"C:\MyVideo_2.mp4", "/htdocs/MyVideo_2.mp4");
// compare the downloaded file with the server
if (client.CompareFile(@"C:\MyVideo_2.mp4", "/htdocs/MyVideo_2.mp4") == FtpCompareResult.Equal){ }
// delete the file
client.DeleteFile("/htdocs/MyVideo_2.mp4");
// upload a folder and all its files
client.UploadDirectory(@"C:\website\videos\", @"/public_html/videos", FtpFolderSyncMode.Update);
// upload a folder and all its files, and delete extra files on the server
client.UploadDirectory(@"C:\website\assets\", @"/public_html/assets", FtpFolderSyncMode.Mirror);
// download a folder and all its files
client.DownloadDirectory(@"C:\website\logs\", @"/public_html/logs", FtpFolderSyncMode.Update);
// download a folder and all its files, and delete extra files on disk
client.DownloadDirectory(@"C:\website\dailybackup\", @"/public_html/", FtpFolderSyncMode.Mirror);
// delete a folder recursively
client.DeleteDirectory("/htdocs/extras/");
// check if a file exists
if (client.FileExists("/htdocs/big2.txt")){ }
// check if a folder exists
if (client.DirectoryExists("/htdocs/extras/")){ }
// upload a file and retry 3 times before giving up
client.RetryAttempts = 3;
client.UploadFile(@"C:\MyVideo.mp4", "/htdocs/big.txt", FtpRemoteExists.Overwrite, false, FtpVerify.Retry);
// disconnect! good bye!
client.Disconnect();
This code demonstrates how to connect to an FTP server and perform FTP operations using async
/await
syntax in .NET 4.5 and later.
// create an FTP client and specify the host, username and password
// (delete the credentials to use the "anonymous" account)
FtpClient client = new FtpClient("123.123.123.123", "david", "pass123");
// connect to the server and automatically detect working FTP settings
await client.AutoConnectAsync();
// get a list of files and directories in the "/htdocs" folder
foreach (FtpListItem item in await client.GetListingAsync("/htdocs")) {
// if this is a file
if (item.Type == FtpFileSystemObjectType.File){
// get the file size
long size = await client.GetFileSizeAsync(item.FullName);
// calculate a hash for the file on the server side (default algorithm)
FtpHash hash = await client.GetChecksumAsync(item.FullName);
}
// get modified date/time of the file or folder
DateTime time = await client.GetModifiedTimeAsync(item.FullName);
}
// upload a file
await client.UploadFileAsync(@"C:\MyVideo.mp4", "/htdocs/MyVideo.mp4");
// move the uploaded file
await client.MoveFileAsync("/htdocs/MyVideo.mp4", "/htdocs/MyVideo_2.mp4");
// download the file again
await client.DownloadFileAsync(@"C:\MyVideo_2.mp4", "/htdocs/MyVideo_2.mp4");
// compare the downloaded file with the server
if (await client.CompareFileAsync(@"C:\MyVideo_2.mp4", "/htdocs/MyVideo_2.mp4") == FtpCompareResult.Equal){ }
// delete the file
await client.DeleteFileAsync("/htdocs/MyVideo_2.mp4");
// upload a folder and all its files
await client.UploadDirectoryAsync(@"C:\website\videos\", @"/public_html/videos", FtpFolderSyncMode.Update);
// upload a folder and all its files, and delete extra files on the server
await client.UploadDirectoryAsync(@"C:\website\assets\", @"/public_html/assets", FtpFolderSyncMode.Mirror);
// download a folder and all its files
await client.DownloadDirectoryAsync(@"C:\website\logs\", @"/public_html/logs", FtpFolderSyncMode.Update);
// download a folder and all its files, and delete extra files on disk
await client.DownloadDirectoryAsync(@"C:\website\dailybackup\", @"/public_html/", FtpFolderSyncMode.Mirror);
// delete a folder recursively
await client.DeleteDirectoryAsync("/htdocs/extras/");
// check if a file exists
if (await client.FileExistsAsync("/htdocs/big2.txt")){ }
// check if a folder exists
if (await client.DirectoryExistsAsync("/htdocs/extras/")){ }
// upload a file and retry 3 times before giving up
client.RetryAttempts = 3;
await client.UploadFileAsync(@"C:\MyVideo.mp4", "/htdocs/big.txt", FtpRemoteExists.Overwrite, false, FtpVerify.Retry);
// disconnect! good bye!
await client.DisconnectAsync();
Now that you've seen the basic usage, check out the other examples in C# or VB:
- Auto Connection
- Auto Reconnection
- FTP(S) Connection
- FTP(S) Connection using GnuTLS
- FTPS Proxies
- Custom Servers
- Custom Commands
- v40 Migration Guide