Click here to Skip to main content
15,867,835 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 569.2K   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

 
GeneralRe: Change file permissions Pin
Andrlage3-May-07 12:57
Andrlage3-May-07 12:57 
GeneralSwitch passive mode! Pin
Marc Schneider9-Feb-04 6:21
Marc Schneider9-Feb-04 6:21 
GeneralShould implement IDisposable Pin
Ed Brey3-Feb-04 0:08
Ed Brey3-Feb-04 0:08 
GeneralGr8 one.... but two big bugs Pin
mad050311-Jan-04 16:34
mad050311-Jan-04 16:34 
GeneralRe: Gr8 one.... but two big bugs Pin
dtelford7-Apr-04 11:40
dtelford7-Apr-04 11:40 
GeneralWorks fine for me ! Pin
lurkbat9-Jan-04 2:10
lurkbat9-Jan-04 2:10 
GeneralBug in readLine() Pin
FifaRules30-Dec-03 7:21
FifaRules30-Dec-03 7:21 
General11226 Transfer Complete. throws FTPException Pin
Aleksandr Maus17-Dec-03 17:42
Aleksandr Maus17-Dec-03 17:42 
Generalpassive mode Pin
ic3man17-Dec-03 7:21
ic3man17-Dec-03 7:21 
Generalbug found in FTPFactory.cs Pin
ooooops16-Dec-03 3:51
ooooops16-Dec-03 3:51 
QuestionAsync method tool? Pin
Nathan Davis21-Nov-03 7:39
Nathan Davis21-Nov-03 7:39 
QuestionIs there a better workaround for timing problems? Pin
faylore27-Oct-03 19:02
faylore27-Oct-03 19:02 
AnswerRe: Is there a better workaround for timing problems? Pin
mcbevin11-Nov-03 11:24
mcbevin11-Nov-03 11:24 
GeneralRe: Is there a better workaround for timing problems? Pin
reubenf17-May-04 11:44
reubenf17-May-04 11:44 
GeneralExcellent job Pin
Riedwhan20-Oct-03 22:16
Riedwhan20-Oct-03 22:16 
GeneralSlow over internet Pin
Andrew Jones11-Oct-03 1:12
Andrew Jones11-Oct-03 1:12 
GeneralProblem downloading big files Pin
Anonymous9-Oct-03 18:54
Anonymous9-Oct-03 18:54 
GeneralProblem Downloading after ChangeDir Pin
John Gerding25-Sep-03 4:58
John Gerding25-Sep-03 4:58 
GeneralGoddamme it,it's pretty good =) Pin
SuMa_cn19-Sep-03 16:49
sussSuMa_cn19-Sep-03 16:49 
Generali like it Pin
winux7-Aug-03 7:40
winux7-Aug-03 7:40 
Questionnice job, but where is the directory listing? Pin
raul_cel_mare12-Jul-03 22:50
raul_cel_mare12-Jul-03 22:50 
GeneralAwesome! Pin
Jamie Nordmeyer27-Jun-03 9:50
Jamie Nordmeyer27-Jun-03 9:50 
GeneralNice Job, but a few comments... Pin
pbible16-Jun-03 12:24
pbible16-Jun-03 12:24 
GeneralRe: Nice Job, but a few comments... Pin
MrBumpy2-Feb-04 1:48
MrBumpy2-Feb-04 1:48 
GeneralRe: Nice Job, but a few comments... Pin
dtelford7-Apr-04 11:42
dtelford7-Apr-04 11:42 

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.