csharp-fluentftp

How to Use FTP from C# with FTP Fluent

  • 3 min

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

The library is based on the FTP protocol (File Transfer 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 regarding FTP.

Furthermore, it has a lot of options that we can use to customize its behavior to our project’s needs. Finally, it also features an integrated error control system that helps us manage potential errors during communication.

How to Use FluentFTP

We can add the library to a .NET project easily, 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 getting file information.

// 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();
Copied!

Here is a compilation of some 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/");
Copied!