FTP Client Library For C#

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Printed from http://www.developerfusion.co.

uk/show/4340/

FTP client library for C#


Rated
Viewed 41089 times
Author Dan Glass
Actions
Save as Favourite
Finding a fully working, lightweight ftp client that had no GUI, was free, and came with
Share with a Friend source was difficult. This API is based on one Jaimon Mathew had done, and I have
PDF Version added some methods, cleaned up the code, debugged it, added some error handling,
Report Problem 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.

Related Content
A Chat Client/Server Program for C#
Socket Programming in C# - Part 1
Socket Programming in C# - Part 2
How to POP3 in C#
A Console IRC Bot
Introduction to TCP/IP
Mastering IIS FTP

PDF Generated by ABCpdf 5.0

© Copyright 1999-2005 Developer Fusion Ltd

You might also like