Click here to Skip to main content
16,006,605 members
Articles / Programming Languages / C#

C# FTP Client Library

Rate me:
Please Sign up or sign in to vote.
4.65/5 (24 votes)
23 May 2007CPOL1 min read 271.2K   4.3K   124   83
Easy to use FTP client library with features in mind.

Introduction

This code is an FTP class library for adding FTP access to your projects. It is heavily based on a project (see background for original code), and is now contained in the source files. I was not sure how to update the original article or add to it so I am publishing this as a new article since there was an extensive re-work of the original code in an attempt to make it more developer-friendly.

Background

This article, and code, are based upon the ftplib posted by J.P. Trosclair. A link to the article is here. I have left original copyright and credit notices in with the updated version of the code.

Using the code

I used nDoc to create a CHM file (in the doc directory of the code) to help with implementation. Its fairly simple, and you will mainly need basic FTP connection information (IP, username, etc.) to use the library.

An example is included in the documentation, and appears as so (to upload a file):

C#
OpenFTP.FTP f = new OpenFTP.FTP();
f.Connect("127.0.0.1", "username_here", "password_here");
f.ChangeDirectory("somedirectory");
f.Files.Upload(Path.GetFileName(sFileName), Path.GetFileName(sFileName));

while (!f.Files.UploadComplete)
{
    Console.WriteLine("Uploading: TotalBytes: " + 
        f.Files.TotalBytes.ToString() + ", : PercentComplete: " + 
        f.Files.PercentComplete.ToString());
}
Console.WriteLine("Upload Complete: TotalBytes: " + 
    f.Files.TotalBytes.ToString() + ", : PercentComplete: " + 
    f.Files.PercentComplete.ToString());

f.Disconnect();
f = null;

Points of Interest

I liked the original article but decided that even though I could read and implement the code I wanted its implementation to be more simplistic so months later I won't have to re-read any code to figure how to include the methods in a new project. I have tried to remove the rough edges.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Anonymous Proxy Anonymous Proxy
I am a Senior-level .NET developer using C# for about 5+ years now.

Codeproject has been a tremendous resource for me over the years.

Comments and Discussions

 
QuestionHow to connect to two FTP Servers Pin
LearnNetnAll15-Dec-08 21:59
LearnNetnAll15-Dec-08 21:59 
GeneralA socket operation encountered a dead network Pin
josephx2k715-Dec-08 12:41
josephx2k715-Dec-08 12:41 
Generalgood library Pin
emilioarp12-Nov-08 7:30
emilioarp12-Nov-08 7:30 
Generalconnection was forcibly closed by the remote host at System.Net.Sockets. Pin
next100years11-Nov-08 0:55
next100years11-Nov-08 0:55 
GeneralA comfortable FTP class in .NET Pin
Elmue27-Aug-08 12:22
Elmue27-Aug-08 12:22 
GeneralChangeDirectory Chrash Pin
Member 42541521-May-08 11:52
Member 42541521-May-08 11:52 
GeneralRe: ChangeDirectory Chrash Pin
jmers3-May-08 9:36
jmers3-May-08 9:36 
GeneralRe: ChangeDirectory Chrash Pin
streit31-May-08 13:13
streit31-May-08 13:13 
Hi,

I think you have done nothing wrong. I also have problems with ChangeDirectory("xxx"), getting error message "Input string was not in a correct format" which is caused by the given below function. It seems as this function expects a completly different structured line.

///
/// Parse and Add a New File to the Collection
///

/// <param name="sUnparsedDirectory" />Unparsed File Entry String from the FTP Server
public void Add(string sUnparsedFile)
{
string sFileName = sUnparsedFile.Substring(39).Trim().Replace("\r","");
long lFileSize = long.Parse(sUnparsedFile.Substring(17, 21).Trim()); // <--- this line causes the problem
DateTime dtFileDate = Convert.ToDateTime(sUnparsedFile.Substring(0, 8) + " " + sUnparsedFile.Substring(10, 7));
Add(sFileName, lFileSize, dtFileDate);
}
GeneralRe: ChangeDirectory Chrash Pin
emilioarp2-Oct-08 8:47
emilioarp2-Oct-08 8:47 
GeneralRe: ChangeDirectory Chrash Pin
B.Ritter3-Nov-08 9:50
B.Ritter3-Nov-08 9:50 
GeneralRe: ChangeDirectory Chrash [modified] Pin
B.Ritter8-Nov-08 2:42
B.Ritter8-Nov-08 2:42 
GeneralRe: ChangeDirectory Chrash Pin
emilioarp10-Nov-08 8:18
emilioarp10-Nov-08 8:18 
GeneralRe: ChangeDirectory Chrash Pin
B.Ritter10-Nov-08 22:12
B.Ritter10-Nov-08 22:12 
GeneralRe: ChangeDirectory Chrash Pin
emilioarp12-Nov-08 4:24
emilioarp12-Nov-08 4:24 
GeneralRe: ChangeDirectory Chrash Pin
jose.mira27-Apr-09 7:38
jose.mira27-Apr-09 7:38 
GeneralRe: ChangeDirectory Chrash Pin
jose.mira4-May-09 7:01
jose.mira4-May-09 7:01 
Generalerror connecting the ftpserver Pin
nanio31-Jan-08 20:56
nanio31-Jan-08 20:56 
GeneralRe: error connecting the ftpserver Pin
jmers30-Apr-08 5:57
jmers30-Apr-08 5:57 
GeneralSubdirectories when uploading Pin
dannywooly21-Nov-07 0:50
dannywooly21-Nov-07 0:50 
GeneralRe: Subdirectories when uploading Pin
jmers21-Nov-07 4:24
jmers21-Nov-07 4:24 
GeneralFiles download Pin
idanz24-Oct-07 22:05
idanz24-Oct-07 22:05 
GeneralRe: Files download Pin
jmers25-Oct-07 1:13
jmers25-Oct-07 1:13 
GeneralRe: Files download Pin
idanz25-Oct-07 3:09
idanz25-Oct-07 3:09 
GeneralRe: Files download Pin
jmers25-Oct-07 3:43
jmers25-Oct-07 3:43 
GeneralRe: Files download Pin
idanz25-Oct-07 4:15
idanz25-Oct-07 4:15 

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.