Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Team Foundation Server Hosting
Search :       Advanced Search »
Home » Internet & Web » FTP Client Library for C#

FTP Client Library for C#

Finding a fully working, lightweight FTP Client that had no GUI, was free, and came with source was difficult.

Page Views : 121401
Downloads : 7095
Rating :
 Rate it
Level : Intermediate
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
FtpClient.zip
 
 
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 



Overview

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://ftp.login/();
ftp.Upload(@"C:\image.jpg");
ftp://ftp.close/();

Not so difficult now is it? 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
  • Shortcomings 
  • Not fully tested 
  • No real error handling 
  • No download recurs yet 
  • Rename will overwrite if the target 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://ftp.login/();
ftp://ftp.beginupload(imagepath/, callback);
ftp://ftp.close/();
}
private void CloseConnection(IAsyncResult result)
{
Debug.WriteLine(result.IsCompleted.ToString());
if ( ftp != null ) ftp://ftp.close/();
ftp =
null;
}

When the upload finishes (or throws an exception), the CloseConnection method will be called.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 Article Extensions
Contents added by jomo jutha on Apr 08, 2011
I tried this code out, slightly modified, and I found the upload directory script to be so slow.
can you suggest ways to speed it up?
 [Top] Rate this article
 
 About the author
 
dan
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Comments
ftpclient by Joe On October 24, 2007
which namespace i have to use to get access to FtpClient class.
Reply | Email | Modify 
ftp client by Steve On November 13, 2007
thanks for the library, this is exactly what i was looking for!
Reply | Email | Modify 
ftpclient by liang On December 18, 2007
can i get all directories ?
Reply | Email | Modify 
ftpclient by liang On December 18, 2007
can i get all directories ?
Reply | Email | Modify 
asd by abhi On August 27, 2008
good
Reply | Email | Modify 
Error in code by dEEPESH On September 30, 2008
I am getting error when login The requested name is valid and was found in the database, but it does not have the correct associated data being resolved for on this line of code addr = Dns.Resolve(this.server).AddressList[0]; pls help
Reply | Email | Modify 
Re: Error in code by Mitch On December 31, 2008
I got the same error.. I solved it by replacing
                addr = Dns.Resolve(this.server).AddressList[0];    with
                addr = Dns.GetHostEntry(this.server).AddressList[0];

This line of code has to replaced in 2 places
Reply | Email | Modify 
Update Panel of AJAX by Nisarg On November 6, 2008
can anybody tell me the solution??? I am using the master pages and in the content pages i am using the Update panel control of AJAX But when i run website sometimes i am not getting the contents which i have put in update panel and sometimes its is displayed... this problem occurs specifically in IE6 this works fine with IE7, means contents are getting displayed in IE7 but not in IE6... Please Help me...... Thanx in advance.........
Reply | Email | Modify 
Output by Jamie On May 25, 2009
Is it possabe to get the output of this control e.g. wrong password, file uploaded, server does not exist etc

Thanks
Reply | Email | Modify 
How to use? by kik On July 24, 2009
I'm a beginner and i was having some trouble with this. altough the class works fine when I use the upload() method it tries to find the file on the server computer (since asp.net works onthe server). I was wondering how to get the file from the users computer. i know this is kind of stupid but pls bear with me i'm only a beginner experimenting and learning
Reply | Email | Modify 
Re: How to use? by Pantelis On December 3, 2009
Lets see, if I get it right. You are probably writing an ASP.NET application which runs on a server (lets say server A) and you want the user to be able to upload a file through FTP to another server (lets sey server B) right?

First of all, your code runs on server A where your ASP.NET application is executed. When you call the upload method, the computer that runs this code acts as an FTP client. If you want to upload a file from the client PC side to server A, you could do it using the UploadFile control. If you want to run some client side code, you could do it through javascript but I am not sure if you could trigger an FTP upload.

Hope this helps a little...
Reply | Email | Modify 
is it for window mobile ce 5 ? by Muhammad On April 7, 2010
hi

Is it workable for window mobile ce 5. I am looking ftp client class for mobile application in c#.

regards,

Ahsan shakir
Reply | Email | Modify 
speedingup the client by jomo On April 12, 2011
the uploading of a folder is VERY slow, i am wondering how to speed it up. what would be your suggestion ?
Reply | Email | Modify 
Re: speedingup the client by Muhammad On April 12, 2011
Use WinSCP.
Reply | Email | Modify 
Good working C# FTP class by Kevin On April 13, 2011
Hey, i found a good working ftp class for c# at http://kevingerndt.blogspot.com/2011/04/ftp-klasse-fur-c.html.
Reply | Email | Modify 
Performance of Upload file by MATHANPRABU On May 21, 2011
Is there any way to increase performance of upload file?
Reply | Email | Modify 
Re: Performance of Upload file by jomo On May 21, 2011
I think as far as a single file upload is concerned..i don't know. but for a folder upload, following what most ftp programs do, I think one would need to open several connections at once to upload several files in a folder
Reply | Email | Modify 
Team Foundation Server Hosting
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.