Click here to Skip to main content
Licence 
First Posted 27 Mar 2003
Views 293,063
Bookmarked 189 times

FTP client library for C#

By | 27 Mar 2003 | Article
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:

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

Tech Support

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

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionError Async Download!!! PinmemberEdward Vargas9:53 28 Mar '12  
QuestionProblem using FTP Client on machines with Norton 360 Antivirus software Pinmemberandreyakub21:48 24 Oct '11  
GeneralFix error "Object reference not set to an instance of an object" when using class FtpWebRequest on Net 4.0 Pinmemberqdhoang21:31 8 Mar '11  
GeneralGood work Pinmemberlapeau3:08 21 Sep '10  
GeneralProblem With IPv6 and Possible Solution PinmemberGaroRobe14:34 15 Sep '10  
GeneralError while uploading large files. Pinmemberfaisaliqbalch6:37 14 Apr '10  
GeneralRe: Error while uploading large files. PinmemberComponentSoft15:37 14 Jul '10  
GeneralQuick Question Pinmembervijayr3:04 8 Apr '10  
GeneralRe: Quick Question Pinmemberdaveauld2:03 9 Apr '10  
QuestionError When Trying Login PinmemberSibiSurya21:36 13 Jan '10  
Questiondownload problem Pinmembernew start0:19 6 Nov '09  
AnswerRe: download problem Pinmembernew start0:40 6 Nov '09  
GeneralThanks dude Pinmemberfeisal5:17 30 Sep '09  
QuestionThe Max number of Uploaded files Pinmemberfhy1977022720:46 29 Jul '09  
Rantdownload doesnt work Pinmemberprozomg5:03 8 Jan '09  
GeneralRe: download doesnt work PinmemberEdward Vargas9:57 28 Mar '12  
Generalthis ftpserver is good! Pinmemberhouzhifa1:02 9 Oct '08  
Generalthis message Pinmemberyudi01020:43 24 Sep '08  
GeneralA comfortable FTP class in .NET PinmemberElmue12:16 27 Aug '08  
Generalimpliment progressbar Pinmemberarapillai3:32 11 Aug '08  
QuestionSecur Connection PinmemberSaeidehV20:35 11 Sep '07  
AnswerRe: Secur Connection Pinmemberkorrawit7:51 6 Nov '07  
QuestionPossibility to use QUOTE RCMD SBMJOB CMD()? Pinmemberpaoloden0:58 6 Jan '07  
Questionhow to run with chinese file and folder name Pinmemberfnjcr19:36 7 Dec '06  
AnswerRe: how to run with chinese file and folder name Pinmembertianjh8316:01 8 Feb '07  

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.

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