Click here to Skip to main content
13,290,735 members (81,290 online)
Click here to Skip to main content
Add your own
alternative version


178 bookmarked
Posted 7 May 2002

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

You may also be interested in...


Comments and Discussions

QuestionBug Report: the method FtpDirectory.FindFile is always case-sensitive! Pin
Dr.Benj8-Apr-12 21:11
memberDr.Benj8-Apr-12 21:11 
QuestionBug Report: the FtpFile.Rename method is empty Pin
Dr.Benj8-Apr-12 21:08
memberDr.Benj8-Apr-12 21:08 
BugI had Fixed Some Bug of this Ftp Lib Pin
williechen16-Dec-11 0:59
memberwilliechen16-Dec-11 0:59 
QuestionNot all directories are listet - UnixListLineExpression needs any change Pin
Nasenbaaer16-Nov-11 1:13
memberNasenbaaer16-Nov-11 1:13 
AnswerSolution found Pin
Nasenbaaer16-Nov-11 22:56
memberNasenbaaer16-Nov-11 22:56 
GeneralDirectory timestamp regex... Pin
therearefartoomanybens13-Dec-09 15:50
membertherearefartoomanybens13-Dec-09 15:50 
GeneralError :Could not find file link.text Pin
massu23-Feb-09 3:25
membermassu23-Feb-09 3:25 
Generalfile transfer Pin
guptasahil198817-Feb-09 23:19
memberguptasahil198817-Feb-09 23:19 
QuestionCannot List FTP Directories Pin
Gulfraz Khan15-Dec-08 23:23
memberGulfraz Khan15-Dec-08 23:23 
GeneralDirectory Tree download.. Pin
Member 439131128-Sep-08 20:08
memberMember 439131128-Sep-08 20:08 
GeneralA comfortable FTP class in .NET Pin
Elmue27-Aug-08 13:16
memberElmue27-Aug-08 13:16 
QuestionHow can i connect to\someFolder? Pin
ipop007zZZ7-Jul-08 0:10
memberipop007zZZ7-Jul-08 0:10 
AnswerRe: How can i connect to\someFolder? Pin
therearefartoomanybens13-Dec-09 10:44
membertherearefartoomanybens13-Dec-09 10:44 
QuestionHow create directory Pin
alien25011-Sep-07 3:23
memberalien25011-Sep-07 3:23 
AnswerRe: How create directory Pin
alien25011-Sep-07 4:17
memberalien25011-Sep-07 4:17 
GeneralRe: How create directory Pin
Member 281270421-Jun-11 3:11
memberMember 281270421-Jun-11 3:11 
GeneralParent Directory Pin
Lava0413-Jun-07 1:24
memberLava0413-Jun-07 1:24 
QuestionHow to move a file from one directory to another directory in ftp Pin
chandu.sanka28-Nov-06 3:17
memberchandu.sanka28-Nov-06 3:17 
Questionhow to trace the target location directory Pin
chandu.sanka28-Nov-06 3:13
memberchandu.sanka28-Nov-06 3:13 
Questionkcommon.dll Pin
_anagram_1-Nov-06 3:32
member_anagram_1-Nov-06 3:32 
AnswerRe: kcommon.dll Pin
_anagram_1-Nov-06 7:12
member_anagram_1-Nov-06 7:12 
QuestionRe: kcommon.dll Pin
alexander_z212-Jul-11 15:38
memberalexander_z212-Jul-11 15:38 
QuestionHow to download from ftp server using VS2003 Pin
Raju Sharma17-Oct-06 21:21
memberRaju Sharma17-Oct-06 21:21 
Generalftp upload Pin
preets30816-Jul-06 21:15
memberpreets30816-Jul-06 21:15 
GeneralCoping file to different directory in remote server Pin
jasocl3-Jul-06 23:11
memberjasocl3-Jul-06 23:11 
AnswerRe: Coping file to different directory in remote server Pin
stixoffire10-Apr-08 6:18
memberstixoffire10-Apr-08 6:18 
GeneralUNIX regex's not recognising all files returned by LIST command - fix supplied here!! Pin
Stewart Rae8-May-06 16:58
memberStewart Rae8-May-06 16:58 
GeneralRe: UNIX regex's not recognising all files returned by LIST command - fix supplied here!! Pin
Shorin_gr15-Jun-06 5:14
memberShorin_gr15-Jun-06 5:14 
GeneralRe: UNIX regex's not recognising all files returned by LIST command - fix supplied here!! Pin
AquaJo13-Jul-06 10:14
memberAquaJo13-Jul-06 10:14 
GeneralRe: UNIX regex's not recognising all files returned by LIST command - fix supplied here!! Pin
josias_c28-Oct-10 10:20
memberjosias_c28-Oct-10 10:20 
GeneralRe: UNIX regex's not recognising all files returned by LIST command - fix supplied here!! Pin
lesnikowski4-Jan-11 8:39
memberlesnikowski4-Jan-11 8:39 
GeneralCannot connect .. Help !! Pin
Star999919-Apr-06 18:18
memberStar999919-Apr-06 18:18 
GeneralSubdirectory Pin
nmbvc2-Mar-06 6:01
membernmbvc2-Mar-06 6:01 
GeneralRe: Subdirectory Pin
therearefartoomanybens13-Dec-09 15:51
membertherearefartoomanybens13-Dec-09 15:51 
Questioncan't download demo file? Pin
Anonymous6-Oct-05 14:39
sussAnonymous6-Oct-05 14:39 
Confused | :confused: Hi,
I am very interested in this. ButHow come i can't download the demo file?


GeneralHi Alex Pin
santosh poojari14-Sep-05 21:40
membersantosh poojari14-Sep-05 21:40 
GeneralHelp!! Problem Kcommon GetFile Pin
Jugkrit_Emp@hotmail.com22-Aug-05 22:07
memberJugkrit_Emp@hotmail.com22-Aug-05 22:07 
GeneralRe: Help!! Problem Kcommon GetFile Pin
Anonymous29-Aug-05 1:58
sussAnonymous29-Aug-05 1:58 
Generalavoid blocking while reading response stream! Pin
[pascal]17-Aug-05 5:59
member[pascal]17-Aug-05 5:59 
GeneralRe: avoid blocking while reading response stream! Pin
cphexad13-Feb-07 4:44
membercphexad13-Feb-07 4:44 
GeneralHi Help needed Pin
NR_gopal3-Aug-05 2:40
memberNR_gopal3-Aug-05 2:40 
Generalunhandled exceptions Pin
[pascal]27-Jul-05 7:24
member[pascal]27-Jul-05 7:24 
Generalpatch for linux ftp list parsing Pin
[pascal]25-Jul-05 6:57
member[pascal]25-Jul-05 6:57 
GeneralRe: patch for linux ftp list parsing Pin
cwetzelberger8-May-07 5:41
membercwetzelberger8-May-07 5:41 
Generaluploading images to ftp using files Pin
v.venkannababu5-Jun-05 0:44
memberv.venkannababu5-Jun-05 0:44 
Generaldoesnt compile Pin
David Every3-Jun-05 13:20
sussDavid Every3-Jun-05 13:20 
QuestionHow to use this coding in ASP.NET ?? Pin
pathakmakrand@yahoo.com16-May-05 19:54
susspathakmakrand@yahoo.com16-May-05 19:54 
GeneralUnix Regular Expression Pin
SasquatP29-Mar-05 10:34
memberSasquatP29-Mar-05 10:34 
GeneralVPN performance problem-design flaw Pin
fmoldoveanu21-Mar-05 14:28
memberfmoldoveanu21-Mar-05 14:28 
QuestionMultipel-files? Pin
anonymous24-Feb-05 4:13
sussanonymous24-Feb-05 4:13 

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
Web02 | 2.8.171207.1 | Last Updated 8 May 2002
Article Copyright 2002 by Alex Kwok
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid