Language: EN

csharp-fluentftp

How to Use FTP from C# with FTP Fluent

FluentFTP is an open-source .NET library that allows us to interact with FTP servers easily and efficiently from an application in C#.

The library is based on the FTP (File Transfer Protocol) protocol, which is an industry standard for file exchange over the network, although it also supports FTPS (not SFTP).

FluentFTP supports a wide variety of common operations such as uploading and downloading files, creating and deleting directories, hash verification, among many others. In general, it has any operation you might need in terms of FTP.

The library is designed to be intuitive and easy to understand. Its intuitive design and its ability to quickly detect and correct errors make it an excellent choice for developers.

It also has a lot of options that we can use to customize its behavior to the needs of our project. Finally, it also has a built-in error control system that helps us manage potential errors during communication.

How to Use FluentFTP

We can easily add the library to a .NET project through the corresponding Nuget package.

Install-Package FluentFTP

Here are some examples of how to use FluentFTP extracted from the library’s documentation. For example, a basic connection to a server and obtaining information from a file.

// create an FTP client and specify the host, username and password
var client = new FtpClient("123.123.123.123", "username", "pasword");

// 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 == FtpObjectType.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);
}

client.Disconnect();

Here is a compilation of some of the common actions we can perform with FluentFTP

// check if a file exists
if (client.FileExists("/htdocs/big2.txt")) { }

// check if a folder exists
if (client.DirectoryExists("/htdocs/extras/")) { }

// compare the downloaded file with the server
if (client.CompareFile(@"C:\MyVideo_2.mp4", "/htdocs/MyVideo_2.mp4") == FtpCompareResult.Equal) { }

// 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");

// 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);

// upload a file
client.UploadFile(@"C:\MyVideo.mp4", "/htdocs/MyVideo.mp4");

// upload a file and retry 3 times before giving up
client.Config.RetryAttempts = 3;
client.UploadFile(@"C:\MyVideo.mp4", "/htdocs/big.txt", FtpRemoteExists.Overwrite, false, FtpVerify.Retry);

// 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);

// delete the file
client.DeleteFile("/htdocs/MyVideo_2.mp4");

// delete a folder recursively
client.DeleteDirectory("/htdocs/extras/");

FluentFTP is Open Source, and all the code and documentation is available in the project’s repository at https://github.com/robinrodricks/FluentFTP