Click here to Skip to main content
13,351,239 members (53,914 online)
Click here to Skip to main content
Add your own
alternative version


46 bookmarked
Posted 5 Oct 2002

Console FTP in C#

, 7 Mar 2004
Rate this:
Please Sign up or sign in to vote.
A basic FTP client in C#.


This is my first attempt at making a useful application using C# and sockets. The program is far from complete but I think it can do the basics for now (upload, download, resume, append). I want to add as many features as possible to this semi-console version so that way, it will be easy to use it in a windows form. I'm the worst when it comes to [GOOD] coding practices/methods so I'll take any advice on how I can make my code more efficient. I wouldn't mind hearing how other people have designed such programs.

Since the initial post, I have been working on and off on the FTP client. Unfortunately, my HD crashed and all my work so I restarted from what I had submitted last October. I'm very happy to see that a few individuals have sampled by code and posted messages. I'm trying to design the class to be modular, so it can easily be used/imported in/to a GUI or Console program, and simple.

If you find any bugs, please post them or tell me. I found a lot as I redid the project. The picture to the left is using CoreFtp in a console and the right is using ExtFtpIa in a form.

-- Update --

I have again rewritten the code, it is much cleaner and uses less lines of code. Unfortunately, I didn't implement a lot of the features I had in my previous two versions because they were unnecessary.


Some of the cool things that it can do so far is queue upload/download commands, resume/append files and establish a data connection using PASV or PORT. I've also commented most of the functions in XML so you can create a comments webpage.

The library is capable of uploading, downloading, resuming and appending and can establish a data connection using PASV or PORT. Text sent by the client, and text received from the server can be captured by using a System.IO.TextWriter object such as Console.Out, or TextWriter.Null (if you don't want to capture the text).

Unfortunately, I don't have full access to a windows FTP server so I was unable to implement WindowsFileNode.Delete. It will throw a NotImplementedException when invoked.

I also included some other classes in the library to send emails (MIME and regular text e-mails), and to retrieve e-mails using a POP3 server.

The Important Classes and Methods


The FtpConnection.Create method is overloaded. One will take a username and password, and the other will login anonymously.

static void Main(string[] args)
    FtpConnection ftpConn = FtpConnection.Create("", 
      21, Console.Out, Console.Out);


ReceiveReply retrieves the reply sent by the server. The method blocks until the reply has been received. What is returned is an and ArpaReply object which has two important properties, a reply code and a reply message.


FileTransfer is an abstract classes that synchronously or asynchronously, uploads or downloads a file to or from a FTP server. The four main classes that derive from FileTransfer are ActiveFileDownload, PassiveFileUpload, ActiveFileUpload, and PassiveFileUpload. When performing an asynchronous file transfer, an IDataTransferAsyncResult is returned which contains a method to asynchronosuly abort a file transfer, and a property, BytesTransferred, that gets the amount of bytes transferred so far.


SendCommand sends a commands to the FTP server. The command to send is selected using the , FtpCommand enumerator. You place the parameters in a string object.

SendCommand(FtpCommand cmd, string param)


DirectoryList is an abstract class to retrieve a directory listing from a FTP server. The classes derived from it are ActiveDirectoryList and PassiveDirectoryList.


FileNode contains an abstract method, FileNode.FromFtpList , that parses the directory listing received from a FTP server to an array of FileNode objects. I made a class for the UNIX and windows file listing. I'm having problems with parsing the listings from some servers (e.x.: doing a directory listing from will generate errors). I didn't create code for WindowsFileNode.Delete because I don't have a windows FTP server to test it on, so for now, it will throw a NotImplemented exception.

Test Code

static void Main(string[] args)
    FtpConnection ftpConn = FtpConnection.Create("", 21, 
      Console.Out, Console.Out);
    //get directory listing
    DirectoryList dirList = new PassiveDirectoryList(ftpConn);
    byte[] data = dirList.GetList(null, Console.Out, Console.Out);
    //parse directory listing
    string list = System.Text.Encoding.ASCII.GetString(data);
    UnixFileNode[] fileNodes = (UnixFileNode[]) new UnixFileNode().FromFtpList(list, 
    //show listing on console
    foreach(UnixFileNode fileNode in fileNodes)


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralProgress bar Pin
Niklas Henricson23-Apr-09 3:33
memberNiklas Henricson23-Apr-09 3:33 
QuestionSITE Command for FTP Pin
Shaghnati Mnaohar Goud6-Dec-08 14:11
memberShaghnati Mnaohar Goud6-Dec-08 14:11 
Questioncan u given this in vs 2005 Pin
aravind.talasila2-Oct-08 22:54
memberaravind.talasila2-Oct-08 22:54 
GeneralA comfortable FTP class in .NET Pin
Elmue27-Aug-08 13:23
memberElmue27-Aug-08 13:23 

If you search a comfortable and reusable FTP client,

-- which is running on .NET Framework 1.1 or higher,
-- which can automatically put together splitted files on the server,
-- which allows to download only a part of a file on the server,
-- which allows to resume any broken download,
-- which automatically starts a separate thread,
-- which can be aborted any time from your main thread,
-- which supports UTF8 encoded filenames,
-- which has a built-in download scheduler,
-- which has a built-in bandwidth control,
-- which has a built-in preview function for the download of movies,
-- which automatically reconnects the server after an error has occurred,
-- which displays download progress in percent and in bytes and the remaining time,
-- which writes a detailed logging for all operations it does,
-- which is based on Wininet.dll and has one workaround for each of the 4 known Wininet.dll bugs,
-- which is very well tested and bug-free,
-- which is written by a very experienced programmer and has a very clean and well documented sourcecode,

then have a look at this project:

ElmueSoft Partial FTP Downloader[^]

Jokei need a ftp server and client Pin
k3l0020-Apr-08 1:02
memberk3l0020-Apr-08 1:02 
Generalwelcome messages Pin
ilovesour7-Dec-04 11:36
sussilovesour7-Dec-04 11:36 
GeneralAnother free, open-source .NET FTP component Pin
Hans Andersen11-Nov-04 10:11
sussHans Andersen11-Nov-04 10:11 
QuestionHow to use your code? Pin
simonsaysmorphosis21-Sep-04 14:32
membersimonsaysmorphosis21-Sep-04 14:32 
Generalupload problem Pin
Soshell17-Aug-04 6:57
memberSoshell17-Aug-04 6:57 
GeneralRe: upload problem Pin
Hasani17-Aug-04 15:30
memberHasani17-Aug-04 15:30 
Generalupload file help Pin
jrmcdona1-Jul-04 14:43
memberjrmcdona1-Jul-04 14:43 
GeneralRe: upload file help Pin
Hasani21-Jul-04 2:44
memberHasani21-Jul-04 2:44 
Generaltypo? hehe Pin
Brian Mc27-Apr-04 3:59
memberBrian Mc27-Apr-04 3:59 
QuestionHow about support other languages? Pin
woto20-Mar-04 14:07
memberwoto20-Mar-04 14:07 
AnswerRe: How about support other languages? Pin
woto21-Mar-04 5:35
memberwoto21-Mar-04 5:35 
Generalrandom update Pin
Hasani28-May-03 14:30
memberHasani28-May-03 14:30 
GeneralGreat but what about a simple example Pin
nsimeonov28-May-03 3:31
membernsimeonov28-May-03 3:31 
GeneralRe: Great but what about a simple example Pin
Hasani28-May-03 14:19
memberHasani28-May-03 14:19 
GeneralRe: Great but what about a simple example Pin
nsimeonov30-May-03 5:27
membernsimeonov30-May-03 5:27 
GeneralRe: Great but what about a simple example Pin
Hasani31-May-03 10:01
memberHasani31-May-03 10:01 
GeneralRe: Great but what about a simple example Pin
s2ksim25-Mar-04 12:30
members2ksim25-Mar-04 12:30 
GeneralRe: Great but what about a simple example Pin
Hasani25-Mar-04 15:37
memberHasani25-Mar-04 15:37 
GeneralRe: Great but what about a simple example Pin
s2ksim26-Mar-04 4:13
members2ksim26-Mar-04 4:13 
GeneralRe: Great but what about a simple example Pin
Hasani26-Mar-04 21:12
memberHasani26-Mar-04 21:12 
Generalgui raises exception Pin
Tadeusz Dracz23-May-03 23:24
memberTadeusz Dracz23-May-03 23:24 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 8 Mar 2004
Article Copyright 2002 by Hasani
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid