Click here to Skip to main content
Click here to Skip to main content

FTP component written with fully managed code

, 7 May 2002
Rate this:
Please Sign up or sign in to vote.
A .NET FTP component

Recently, I needed to write a program which could automatically download files from an FTP server. Unfortunately the .NET Framework lacks good support for FTP services. Although a few companies have already written libraries which can be used directly, the price is really too high for poor men like me. As a result, I decided to write my own FTP library and here it is.


This FTP component is an incomplete implementation of my design nor has it been fully tested. Please keep in mind it may contain bugs or even design flaws.

Connect to FTP server

FtpSession session = new FtpSession();
session.Server = "localhost";
session.Port   = 21; //not required if using the default ftp port 21
session.Connect("anonymous", "");

Enum subdirectories and files

* It is possible an incorrect list will be returned.
* In this case, you need to add your own regular expression to 
* interpret the list result (try to find it within ftpdirectory.cs)
FtpDirectory root = session.RootDirectory;
foreach(FtpDirectory d in root.SubDirectories)
    //do something

    foreach(FtpFile f in root.Files)
        //do something

         * Only CurrentDirectory's item can be legally used.
         * Do not save down FtpDirectory and FtpFile for later use
         * unless you are sure they belongs to CurrentDirectory.
         * Here is some sample may cause problems
        FtpDirectory d1 = session.CurrentDirectory.FindSubdirectory("somesubdir");
        FtpDirectory d2 = session.CurrentDirectory.FindSubdirectory("othersubdir");
        session.CurrentDirectory = d2;
         * Following 2 line will cause problem since CurrentDirectory 
         * is not d1's parent anymore
        foreach(FtpDirectory d in d1) 

Change current directory

session.CurrentDirectory = somesubdirectory

Upload file

//Blocking call
//Async call using callback
session.CurrentDirectory.BeginPutFile("somelocalfile", +
    new FtpAsyncDelegate(yourcallback);
//Async call using event
session.EndPutFile += new FtpFileEventHandler(yourhandler);
    //Manually upload the file
    FileInfo fi = new FileInfo(localfilefullpath);
    Stream r = File.OpenRead(fi.FullName);

    // actually will return a FtpOutputDataStream object
    Stream w = session.CurrentDirectory.CreateDataStream(fi.Name); 
    int readed;
    byte[] buff = new byte[4096];
    while((readed=r.Read(buff, 0, 4096) != 0)
	    w.Write(buff, 0, readed);
    * Must call FtpDataStream.Close().
    * This is because it will try to read the file transfer 
    * result from FTP server.

Download file

//Blocking call
//Async call using callback
session.CurrentDirectory.BeginGetFile("someremotefile", + 
    new FtpAsyncDelegate(yourcallback);
//Async call using event
session.EngGetFile += new FtpFileEventHandler(yourhandler);

//Manually download a file
Stream r = File.OpenWrite(localfilepath);

    FtpFile remoteFile = session.CurrentDirectory.FindFile(remotefilename);
    Stream w = remoteFile.GetOutputDataStream();
    int readed;
    byte[] buff = new byte[4096];
    while((readed=r.Read(buff, 0, 4096) != 0)
        w.Write(buff, 0, readed);
    * Must call FtpDataStream.Close().
    * This is because it will try to read the file transfer 
    * result from FTP server.

Create a new file in ftp server

//method 1
//method 2
Stream s = session.CurrentDirectory.CreateFileStream(newfilename);
// do something with s

Delete a file from ftp server

//method 1
//method 2
IFtpItem item = session.CurrentDirectory.FindItem(remotefilename)
if(item != null)

Delete a subdirectory

//method 1

//method 2
IFtpItem item = session.CurrentDirectory.FindItem(sub directoryname)
if(item != null)

Refresh the content of ftp directory

* If you have saved down some some directory item(subdir or ftpfile).
* They will become invalid after you refresh the directory
FtpFile f = session.CurrentDirectory.FindFile(somefile);
// you can not leagally use f anymore after the following line

Rename a file or directory

IFtpItem item = session.FindItem(directoryorfile);
if(item != null)
    session.RenameSubitem(item, newname);

Known Limitations

  • No support for active mode
  • No support for proxy server


  • Removed FileCollection and DirectoryCollection
  • Added support for VB's for each statement
  • VB client can directly access FtpDirectory.Files and FtpDirectory.SubDirectories with for each statement now.

Please send me a email if you find any bugs. Please include at least a description of the bug you have found. It will be nice if a testing program or even a solution is included.

Have fun.


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

Alex Kwok
Web Developer
Hong Kong Hong Kong
No Biography provided

Comments and Discussions

GeneralSubdirectory Pinmembernmbvc2-Mar-06 6:01 
GeneralRe: Subdirectory Pinmembertherearefartoomanybens13-Dec-09 15:51 
Questioncan't download demo file? PinsussAnonymous6-Oct-05 14:39 
GeneralHi Alex Pinmembersantosh poojari14-Sep-05 21:40 
GeneralHelp!! Problem Kcommon GetFile PinmemberJugkrit_Emp@hotmail.com22-Aug-05 22:07 
GeneralRe: Help!! Problem Kcommon GetFile PinsussAnonymous29-Aug-05 1:58 
Generalavoid blocking while reading response stream! Pinmember[pascal]17-Aug-05 5:59 
GeneralRe: avoid blocking while reading response stream! Pinmembercphexad13-Feb-07 4:44 
In fact this problem occurs fairly frequently. It is a race condition that happens when the server outputs two responses directly after each other. This happens especially often with the list command, where the server outputs the list, and then the "OK" message.
Because of the way the stream reader works, it reads ahead in the stream. When you call the constructor for the FTPResponse, you create a new stream reader, and thus discards the old one. This is a serious problem since the previous stream reader has already buffered the data. The new stream reader will block forever, since there is no more data.
The solution is to use just one stream reader, and pass that to the FTPResponse constructor.
Another bug in the FTPResponse reader, is that it cannot parse multiline responses:
220 OK - Features:
It will try to parse the number in each line, and throw an exception when parsing line 2. It is a fairly small fix. Other than that, actually a really nice component.
GeneralHi Help needed PinmemberNR_gopal3-Aug-05 2:40 
Generalunhandled exceptions Pinmember[pascal]27-Jul-05 7:24 
Generalpatch for linux ftp list parsing Pinmember[pascal]25-Jul-05 6:57 
GeneralRe: patch for linux ftp list parsing Pinmembercwetzelberger8-May-07 5:41 
Generaluploading images to ftp using files Pinmemberv.venkannababu5-Jun-05 0:44 
Generaldoesnt compile PinsussDavid Every3-Jun-05 13:20 
QuestionHow to use this coding in ASP.NET ?? Pinsusspathakmakrand@yahoo.com16-May-05 19:54 
GeneralUnix Regular Expression PinmemberSasquatP29-Mar-05 10:34 
GeneralVPN performance problem-design flaw Pinmemberfmoldoveanu21-Mar-05 14:28 
QuestionMultipel-files? Pinsussanonymous24-Feb-05 4:13 
GeneralIncomplete download PinsussAnonymous2-Feb-05 13:18 
GeneralTypo in kcommon FTP class PinmemberWyatt Wong31-Jan-05 15:44 
Generalproxy server support PinmemberJohn Makin6-Jan-05 3:12 
GeneralAnother free, open-source .NET FTP component PinsussHans Andersen11-Nov-04 12:45 
GeneralRe: Another free, open-source .NET FTP component Pinmember[pascal]27-Jul-05 7:17 
GeneralcAse-sEnSItiVe regular expressions Match Pinmemberttocek8-Nov-04 10:50 
Generalrequired windows application for uploading last modified files/folders from [Windows] to [WEB] Pinmembersharad1625-Sep-04 4:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150302.1 | Last Updated 8 May 2002
Article Copyright 2002 by Alex Kwok
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid