Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 75,254 times

Downloads

Related Categories

FTP client library for C#

danglass

Finding a fully working, lightweight ftp client that had no GUI, was free, and came with source was difficult. This API is based on one Jaimon Mathew had done, and I have added some methods, cleaned up the code, debugged it, 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.

I started out wrapping fully the WinInet API, but it was sucha labourous task that it made sense to just to the FTP, sinse 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

Short-Comings

  • Not fully tested
  • No real error handling
  • No download recurse yet
  • Rename will overwrite if the targey already exists

Asynchronous operation

A little more advanced, the asynchronous operation is for when you need the job to fork while your code continues over the method. All asynchronous method start with Begin.

Using it is simple. You need a couple of things, an AsyncCallback object and a method which it handles, like so:

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.

Comments

  • Re: [4340] FTP client library for C#

    Posted by vglass on 27 Nov 2006

    may wish to look at the following component.  Includes support for ftp and ftps (ftp over ssl).

    http://www.jscape.com/sftpdotnet/

  • bug in Download()

    Posted by rcole02 on 10 May 2005

    There's a bug in the download while loop.
    The test is looking at timeout and should be changed to:

    while (this.bytes>0)
    {
       this.bytes = cSocket.Receive(buffer, buffer.Length, 0);
       output.Write...

  • PASV VS. PORT

    Posted by bimboy on 02 May 2005

    is there anyone here knows how to use the ftp image upload code... instead of it connecting through PASV.... is there anyway i could use PORT instead...
    i hope this time someone responds to my quest...

  • file upload problem

    Posted by bimboy on 25 Apr 2005

    Can't connect to remote serverSystem.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection fai...

  • PASV VS. PORT

    Posted by bimboy on 22 Apr 2005

    is there anyone here knows how to use the ftp image upload code... instead of it connecting through PASV.... is there anyway i could use PORT instead...
    i hope this time someone responds to my questi...