Click here to Skip to main content
11,410,118 members (62,432 online)
Click here to Skip to main content

FTP client library for C#

, 27 Mar 2003
Rate this:
Please Sign up or sign in to vote.
FTP client library for C#, including asynchronous operations.


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:

FtpClient ftp = new FtpClient(FtpServer,FtpUserName,FtpPassword);

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.


  • Upload
  • Recursive upload
  • Download
  • Resume
  • Delete
  • Rename
  • Create directory
  • Asynchronous operation


  • 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.

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.BeginUpload(imagePath, callback);

private void CloseConnection(IAsyncResult result)

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

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


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


About the Author

Dan Glass
Australia Australia

Comments and Discussions

GeneralRe: Don't loose your time... Pinmembernietzsche4413-Sep-07 23:19 
GeneralRe: Don't loose your time... Pinmemberjay_t5516-Jan-10 20:43 
Generalerror,while ftp.GetFileList(); Pinmemberlovemory23-Aug-05 18:50 
AnswerRe: error,while ftp.GetFileList(); PinmemberSprotty11-Oct-05 1:13 
GeneralRe: error,while ftp.GetFileList(); PinmemberPatrice Dargenton27-Jun-06 1:40 
GeneralRe: error,while ftp.GetFileList(); Pinmembertmck7-Mar-07 6:25 
GeneralRe: error,while ftp.GetFileList(); Pinmemberti_hugo31-May-07 2:31 
GeneralRe: error,while ftp.GetFileList(); PinmemberPatrick Mortara20-Nov-07 7:25 
The problem lies in the while loop that receives the file list from the server:

while( timeout > DateTime.Now )
int bytes = cSocket.Receive(buffer, buffer.Length, 0);
this.message += ASCII.GetString(buffer, 0, bytes);

if ( bytes < this.buffer.Length ) break;

It exits whenever the number of received bytes is less than the
size of the buffer. Change the 'break' line to:

if ( bytes == 0 ) break;

and you should see the whole directory listing.

I'm no expert for sockets and this stuff too, this is just a hack that worked
for me. I think this whole loop (and probably all similar loops within this class)
should be rewritten. (The timeout condition seems to be nonsense since the
socket is blocking.)
GeneralProxy Support for FTP client Pinmemberdrkalucard4-May-05 0:40 
GeneralAnother free, open-source .NET FTP component PinsussHans Andersen11-Nov-04 12:45 
QuestionHave any body here actualy looked at the code? PinmemberArnt1-Nov-04 13:31 
GeneralNot working Pinmemberagolovan29-Sep-04 15:50 
GeneralRe: Not working Pinmembermohit bhat18-Nov-04 4:22 
GeneralRe: Not working PinsussAnonymous24-Jan-05 20:06 
GeneralIt doesn't work with the IIS FTP server if a welcome message is set PinmemberJustinCouto9-Jun-04 7:15 
GeneralRe: It doesn't work with the IIS FTP server if a welcome message is set PinmemberJose Lopes da Cruz25-Aug-04 6:34 
GeneralRe: It doesn't work with the IIS FTP server if a welcome message is set PinmemberGoToSleep22-Sep-05 6:41 
QuestionNo File Uploading??? Pinsuss_stormbringer_20-May-04 11:15 
Generalasync Pinmembertoni113-May-04 5:13 
GeneralBetter version. PinmemberDan Glass10-May-04 12:55 
GeneralRe: Better version. PinsussAnonymous11-May-04 6:32 
GeneralRe: Better version. PinsussAnonymous11-May-04 7:14 
GeneralRe: Better version. PinsussAnonymous11-Oct-04 23:11 
GeneralI keep getting 530 Login or Password incorrect PinsussAnonymous10-May-04 12:46 
GeneralThanks a ton. Pinmembersupertedusa21-Apr-04 9:59 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150414.5 | Last Updated 28 Mar 2003
Article Copyright 2003 by Dan Glass
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid