Click here to Skip to main content
15,884,473 members
Articles / Programming Languages / C#
Article

FTP client library for C#

Rate me:
Please Sign up or sign in to vote.
4.77/5 (88 votes)
27 Mar 20031 min read 571K   14.5K   217   110
FTP client library for C#, including asynchronous operations.

Overview

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

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

C#
FtpClient ftp = new FtpClient(FtpServer,FtpUserName,FtpPassword);
ftp.Login();
ftp.Upload(@"C:\image.jpg");
ftp.Close();

Not so difficult now, is it? I am using it in my WebCamService list on this site, so if you want to see it in action, go here.

I started out wrapping fully the WinInet API, but it was such a labourous task that it made sense just to do the FTP. Since 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 recourse yet
  • Rename will overwrite, if the target already exists

Asynchronous operation

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

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

C#
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.Login();
    ftp.BeginUpload(imagePath, callback);
    ftp.Close();
}

private void CloseConnection(IAsyncResult result)
{
    Debug.WriteLine(result.IsCompleted.ToString());

    if ( ftp != null ) ftp.Close();
        ftp = null;
} 

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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Architect support.com
Australia Australia

Comments and Discussions

 
GeneralLike the this statements. Where are directories? Pin
M K5-Jan-06 6:22
M K5-Jan-06 6:22 
GeneralReally Great Pin
chito16-Jun-03 1:46
chito16-Jun-03 1:46 
Generalsftp Pin
barnseyboy2-Apr-03 10:19
barnseyboy2-Apr-03 10:19 
GeneralQuestion about the methods 'Upload' and 'createDataSocket' Pin
lesta2-Apr-03 10:04
lesta2-Apr-03 10:04 
GeneralRe: Question about the methods 'Upload' and 'createDataSocket' Pin
Jamie Nordmeyer3-Feb-04 8:39
Jamie Nordmeyer3-Feb-04 8:39 
GeneralRe: Question about the methods 'Upload' and 'createDataSocket' Pin
Member 972704514-Jan-13 6:22
Member 972704514-Jan-13 6:22 
GeneralBravo Pin
Richard Johnn29-Mar-03 6:38
Richard Johnn29-Mar-03 6:38 
GeneralA 5 from me as well Pin
Marc Clifton28-Mar-03 9:15
mvaMarc Clifton28-Mar-03 9:15 
GeneralAwesome Pin
leppie28-Mar-03 7:22
leppie28-Mar-03 7:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.