Click here to Skip to main content
Licence 
First Posted 27 Mar 2003
Views 281,327
Downloads 2,653
Bookmarked 185 times

FTP client library for C#

By Dan Glass | 27 Mar 2003
FTP client library for C#, including asynchronous operations.
4 votes, 5.0%
1
1 vote, 1.3%
2
4 votes, 5.0%
3
8 votes, 10.0%
4
63 votes, 78.8%
5
4.80/5 - 80 votes
5 removed
μ 4.50, σa 1.79 [?]

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
QuestionProblem using FTP Client on machines with Norton 360 Antivirus software Pinmemberandreyakub22:48 24 Oct '11  
GeneralFix error "Object reference not set to an instance of an object" when using class FtpWebRequest on Net 4.0 Pinmemberqdhoang22:31 8 Mar '11  
GeneralGood work Pinmemberlapeau4:08 21 Sep '10  
GeneralProblem With IPv6 and Possible Solution PinmemberGaroRobe15:34 15 Sep '10  
GeneralError while uploading large files. Pinmemberfaisaliqbalch7:37 14 Apr '10  
GeneralRe: Error while uploading large files. PinmemberComponentSoft16:37 14 Jul '10  
GeneralQuick Question Pinmembervijayr4:04 8 Apr '10  
GeneralRe: Quick Question Pinmemberdaveauld3:03 9 Apr '10  
QuestionError When Trying Login PinmemberSibiSurya22:36 13 Jan '10  
Questiondownload problem Pinmembernew start1:19 6 Nov '09  
AnswerRe: download problem Pinmembernew start1:40 6 Nov '09  
GeneralThanks dude Pinmemberfeisal6:17 30 Sep '09  
QuestionThe Max number of Uploaded files Pinmemberfhy1977022721:46 29 Jul '09  
Rantdownload doesnt work Pinmemberprozomg6:03 8 Jan '09  
Generalthis ftpserver is good! Pinmemberhouzhifa2:02 9 Oct '08  
Generalthis message Pinmemberyudi01021:43 24 Sep '08  
GeneralA comfortable FTP class in .NET PinmemberElmue13:16 27 Aug '08  
Generalimpliment progressbar Pinmemberarapillai4:32 11 Aug '08  
QuestionSecur Connection PinmemberSaeidehV21:35 11 Sep '07  
AnswerRe: Secur Connection Pinmemberkorrawit8:51 6 Nov '07  
QuestionPossibility to use QUOTE RCMD SBMJOB CMD()? Pinmemberpaoloden1:58 6 Jan '07  
Questionhow to run with chinese file and folder name Pinmemberfnjcr20:36 7 Dec '06  
AnswerRe: how to run with chinese file and folder name Pinmembertianjh8317:01 8 Feb '07  
GeneralRe: how to run with chinese file and folder name Pinmemberfhy1977022720:04 7 Jul '09  
GeneralRe: how to run with chinese file and folder name Pinmemberfhy1977022721:29 29 Jul '09  

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
Web01 | 2.5.120206.1 | Last Updated 28 Mar 2003
Article Copyright 2003 by Dan Glass
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid