Click here to Skip to main content
6,629,377 members and growing! (22,554 online)
Email Password   helpLost your password?
General Programming » Internet / Network » FTP     Intermediate

FTP client library for C#

By Dan Glass

FTP client library for C#, including asynchronous operations.
C#.NET 1.0, Win2K, WinXP, Visual Studio, Dev
Posted:27 Mar 2003
Views:207,411
Bookmarked:156 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
78 votes for this article.
Popularity: 8.51 Rating: 4.50 out of 5
4 votes, 5.1%
1
1 vote, 1.3%
2
4 votes, 5.1%
3
7 votes, 9.0%
4
62 votes, 79.5%
5

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


Member
Tech Support
Occupation: Architect
Company: support.com
Location: Australia Australia

Other popular Internet / Network articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 88 (Total in Forum: 88) (Refresh)FirstPrevNext
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  
GeneralHow can I use "BeginGetFileList"? Pinmemberwolfs5:56 21 Sep '06  
GeneralRe: How can I use "BeginGetFileList"? Pinmemberwolfs17:16 25 Sep '06  
GeneralRe: How can I use "BeginGetFileList"? Pinmemberendy22:37 28 May '07  
Questionupload images in bad resolution!!! Pinmemberhamid_m14:05 20 Sep '06  
GeneralHow to Download file from Ftp server Pinmembercscrazy18:30 3 Aug '06  
GeneralView a list of files Pinmemberfetty4102:44 10 Jan '06  
GeneralRe: View a list of files Pinmemberbianyb21:51 19 Jul '07  
GeneralDon't loose your time... Pinmembertiusho23:27 30 Nov '05  
GeneralRe: Don't loose your time... PinmemberJeff Firestone8:18 2 Dec '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 27 Mar 2003
Editor: Smitha Vijayan
Copyright 2003 by Dan Glass
Everything else Copyright © CodeProject, 1999-2009
Web19 | Advertise on the Code Project