FTP Client Library for C#



Overview

Finding a fully working, lightweight FTP client that had no GUI, was free, and came with source was difficult. This API is based on one Jaimon Mathew had done, and I have added some methods, cleaned up the code, debugged it, added some error handling, logging, debugging etc.

It is easy to use. Here is a code example:

FtpClient ftp = new FtpClient(FtpServer,FtpUserName,FtpPassword);
ftp://ftp.login/();
ftp.Upload(@"C:\image.jpg");
ftp://ftp.close/();

Not so difficult now is it? I started out wrapping fully the WinInet API, but it was sucha labourous task that it made sense to just to the FTP, sinse Microsoft has great support for HTTP, I could skip that, and later work on SMTP.

  • Features 
  • Upload 
  • Recursive Upload 
  • Download 
  • Resume 
  • Delete 
  • Rename 
  • Create Directory 
  • Asynchronous operation
  • Shortcomings 
  • Not fully tested 
  • No real error handling 
  • No download recurs yet 
  • Rename will overwrite if the target already exists

Asynchronous operation

A little more advanced, the asynchronous operation is for when you need the job to fork while your code continues over the method. All asynchronous method start with Begin.

Using it is simple. You need a couple of things, an AsyncCallback object and a method which it handles, like so:

private FtpClient ftp = null;
private void UploadPicture(string imagePath)
{
string FtpServer = ConfigurationSettings.AppSettings["FtpServer"];
string FtpUserName = ConfigurationSettings.AppSettings["FtpUserName"];
string FtpPassword = ConfigurationSettings.AppSettings["FtpPassword"];
AsyncCallback callback =
new AsyncCallback(CloseConnection);
ftp =
new FtpClient(FtpServer,FtpUserName,FtpPassword);
ftp://ftp.login/();
ftp://ftp.beginupload(imagepath/, callback);
ftp://ftp.close/();
}
private void CloseConnection(IAsyncResult result)
{
Debug.WriteLine(result.IsCompleted.ToString());
if ( ftp != null ) ftp://ftp.close/();
ftp =
null;
}

When the upload finishes (or throws an exception), the CloseConnection method will be called.


Similar Articles