![]() |
General Programming »
Internet / Network »
FTP
Intermediate
FTP client library for C#By Dan GlassFTP client library for C#, including asynchronous operations. |
C#.NET 1.0, Win2K, WinXP, Visual Studio, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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.
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.
General
News
Question
Answer
Joke
Rant
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 Web17 | Advertise on the Code Project |