Click here to Skip to main content
Click here to Skip to main content

FTP client library for C#

By , 27 Mar 2003
 

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:

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.

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

About the Author

Dan Glass
Architect support.com
Australia Australia
Member

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
SuggestionProblem of performancememberMember 857641628 Jan '13 - 0:08 
GeneralMy vote of 5memberDametry18 Dec '12 - 16:55 
QuestionWhere can I download the library?membercathy liang18 Aug '12 - 19:04 
QuestionError Async Download!!!memberEdward Vargas28 Mar '12 - 9:53 
QuestionProblem using FTP Client on machines with Norton 360 Antivirus softwarememberandreyakub24 Oct '11 - 21:48 
GeneralFix error "Object reference not set to an instance of an object" when using class FtpWebRequest on Net 4.0memberqdhoang8 Mar '11 - 21:31 
GeneralGood workmemberlapeau21 Sep '10 - 3:08 
GeneralProblem With IPv6 and Possible SolutionmemberGaroRobe15 Sep '10 - 14:34 
GeneralError while uploading large files.memberfaisaliqbalch14 Apr '10 - 6:37 
GeneralRe: Error while uploading large files.memberComponentSoft14 Jul '10 - 15:37 
GeneralQuick Questionmembervijayr8 Apr '10 - 3:04 
GeneralRe: Quick Questionmemberdaveauld9 Apr '10 - 2:03 
QuestionError When Trying LoginmemberSibiSurya13 Jan '10 - 21:36 
Questiondownload problemmembernew start6 Nov '09 - 0:19 
AnswerRe: download problemmembernew start6 Nov '09 - 0:40 
GeneralThanks dudememberfeisal30 Sep '09 - 5:17 
QuestionThe Max number of Uploaded filesmemberfhy1977022729 Jul '09 - 20:46 
Rantdownload doesnt workmemberprozomg8 Jan '09 - 5:03 
GeneralRe: download doesnt workmemberEdward Vargas28 Mar '12 - 9:57 
Generalthis ftpserver is good!memberhouzhifa9 Oct '08 - 1:02 
Generalthis messagememberyudi01024 Sep '08 - 20:43 
GeneralA comfortable FTP class in .NETmemberElmue27 Aug '08 - 12:16 
Generalimpliment progressbarmemberarapillai11 Aug '08 - 3:32 
QuestionSecur ConnectionmemberSaeidehV11 Sep '07 - 20:35 
AnswerRe: Secur Connectionmemberkorrawit6 Nov '07 - 7:51 

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

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