[Please leave a comment or suggest anything can improve this project in discussion section. Quality of code is my target, join this project and let's talk about it.]

Project Description

TheFtpLibrary is a C# library based exclusively on .NET Framework. It simplifies working with FTP and abstracts to the programmer the need to set up commands to send via ftp. It also automatically manages login and connection.

How it works

TheFtpLibrary is composed of two core classes: FtpDirectory and FtpFile. To start working with your ftp site just instantiate one of those classes passing as parameter a URI to a folder or file respectively.

FtpFile provides, among others, Delete, Download and Rename methods and exposes Permission and some other properties. It also exposes a reference to the containing folder called ParentFolder that is of FtpDirectory type and gives you access to folder browsing.

FtpDirectory exposes the same Delete, Download and Rename methods, but they act on the whole directory instead of working on a single file. FtpDirectory also provides Subdirectories and Files properties that are collections of FtpDirectory and FtpFile respectively: these two properties allow you to browse and operate on remote object with no more need of provide URIs or paths.

You can also take advantage of LINQ on these properties and browse remote files and folders we actually don't know about beforehand.  

While Subdirectories and Files properties enumerate object found only in current directory, GetAllFiles and GetAllSubdirectories methods retrieve all files and subdirectories on all sublevels.

If we know about a specific folder or path we can avoid LINQing on SubDirectories property and instatiante a new FtpDirectory object or use ChangeDirectory to directly access that folder. We can also cast our FtpDirectory to dynamic and dynamically construct a remote path like this: 

FtpDirectory ftp = new FtpDirectory("ftp://ftp.myserver/myfolder", myCredentials)
FtpDirectory otherDir = ((dynamic)ftp).Subdir1.Subdir2;
otherDir.Download(); // we are on ftp://ftp.myserver/myfolder/Subdir1/Subdir2

In the previous example we basically avoid strings to build our path.

Let's now compare The Ftp Library with another library, let's say C# Ftp Library found on codeplex at this address (http://ftplib.codeplex.com/ )

This is the same task performed with TheFtpLibrary

var credentials = new NetworkCredential( "username", "password" );
var ftp = new FtpDirectory( "ftp.server.com", credentials );

var dir = ftp.ChangeDirectory( "incoming" );
dir.Files.First( file => file.Name == "file.txt" )
         .Download( "downloadPath" );

ftp.ChangeDirectory( "outgoing" )
   .UploadFile( @"c:\localfile.txt" )
   .Rename( "newName.txt" );

foreach( var file in ftp.ChangeDirectory( "incoming/processed" ).Files )
	Console.WriteLine( file.ParentDirectory.Name );
        Console.WriteLine( file.Name );

Last edited Aug 31, 2012 at 5:11 PM by heartbreaka, version 17