Click here to Skip to main content
13,046,679 members (58,273 online)
Click here to Skip to main content
Add your own
alternative version

Stats

436.1K views
11.9K downloads
214 bookmarked
Posted 27 Mar 2003

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.

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

Share

About the Author

Dan Glass
Architect support.com
Australia Australia

You may also be interested in...

Pro

Comments and Discussions

 
Generalerror,while ftp.GetFileList(); Pin
lovemory23-Aug-05 17:50
memberlovemory23-Aug-05 17:50 
AnswerRe: error,while ftp.GetFileList(); Pin
Sprotty11-Oct-05 0:13
memberSprotty11-Oct-05 0:13 
GeneralRe: error,while ftp.GetFileList(); Pin
Patrice Dargenton27-Jun-06 0:40
memberPatrice Dargenton27-Jun-06 0:40 
GeneralRe: error,while ftp.GetFileList(); Pin
tmck7-Mar-07 5:25
membertmck7-Mar-07 5:25 
I'm using logic similar to yours in an FTP application to download all available files in a directory on the Host server that the application logs into. I'm using the command sendCommand("NLST " + mask); (where Mask is "*.*") to get a list of files available for download. The issue I'm encountering is that all files available are not being returned in the return buffer. There are 4 files, all of which have been previously downloaded but when I try to download the files again only 1 file is being returned in the buffer as being available. Has this issue been encountered?

TMCK
GeneralRe: error,while ftp.GetFileList(); Pin
ti_hugo31-May-07 1:31
memberti_hugo31-May-07 1:31 
GeneralRe: error,while ftp.GetFileList(); Pin
Patrick Mortara20-Nov-07 6:25
memberPatrick Mortara20-Nov-07 6:25 
GeneralProxy Support for FTP client Pin
drkalucard3-May-05 23:40
memberdrkalucard3-May-05 23:40 
GeneralAnother free, open-source .NET FTP component Pin
Hans Andersen11-Nov-04 11:45
sussHans Andersen11-Nov-04 11:45 
QuestionHave any body here actualy looked at the code? Pin
Arnt1-Nov-04 12:31
memberArnt1-Nov-04 12:31 
GeneralNot working Pin
agolovan29-Sep-04 14:50
memberagolovan29-Sep-04 14:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Terms of Use | Mobile
Web01 | 2.8.170713.1 | Last Updated 28 Mar 2003
Article Copyright 2003 by Dan Glass
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid