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.
Disclaimer
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;
session.Connect("anonymous", "someone@somewhere.com");
Enum subdirectories and files
FtpDirectory root = session.RootDirectory;
foreach(FtpDirectory d in root.SubDirectories)
foreach(FtpFile f in root.Files)
FtpDirectory d1 = session.CurrentDirectory.FindSubdirectory("somesubdir");
FtpDirectory d2 = session.CurrentDirectory.FindSubdirectory("othersubdir");
session.CurrentDirectory = d2;
foreach(FtpDirectory d in d1)
Change current directory
session.CurrentDirectory = somesubdirectory
Upload file
session.CurrentDirectory.PutFile("somelocalfile");
session.CurrentDirectory.BeginPutFile("somelocalfile", +
new FtpAsyncDelegate(yourcallback);
session.EndPutFile += new FtpFileEventHandler(yourhandler);
session.CurrectDirectory.BeginPutFile("somelocalfile");
FileInfo fi = new FileInfo(localfilefullpath);
Stream r = File.OpenRead(fi.FullName);
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);
w.Close();
s.Close();
Download file
session.CurrentDirectory.GetFile("someremotefile");
session.CurrentDirectory.BeginGetFile("someremotefile", +
new FtpAsyncDelegate(yourcallback);
session.EngGetFile += new FtpFileEventHandler(yourhandler);
session.CurrectDirectory.BeginGetFile("someremotefile");
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);
w.Close();
s.Close();
Create a new file in ftp server
session.CurrentDirectory.CreateFile(newfilename);
Stream s = session.CurrentDirectory.CreateFileStream(newfilename);
s.Close();
Delete a file from ftp server
session.CurrentDirectory.RemoveFile(remotefilename);
IFtpItem item = session.CurrentDirectory.FindItem(remotefilename)
if(item != null)
session.CurrentDirectory.RemoveItem(item);
Delete a subdirectory
session.CurrentDirectory.RemoveSubdir(subdirectoryname);
IFtpItem item = session.CurrentDirectory.FindItem(sub directoryname)
if(item != null)
session.CurrentDirectory.RemoveItem(item);
Refresh the content of ftp directory
FtpFile f = session.CurrentDirectory.FindFile(somefile);
session.CurrentDirectory.Refresh();
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
Updates
- 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.