使用完全托管代码编写的 FTP 组件






4.78/5 (54投票s)
2002年5月8日
1分钟阅读

928099

14691
一个 .NET FTP 组件
最近,我需要编写一个程序,它可以自动从 FTP 服务器下载文件。不幸的是,.NET Framework 对 FTP 服务的支持不够好。虽然有一些公司已经编写了可以直接使用的库,但价格对于像我这样的人来说实在太高了。因此,我决定编写自己的 FTP 库,现在它完成了。
免责声明
这个 FTP 组件是我的设计的不完整实现,也没有经过充分的测试。请记住,它可能包含错误甚至设计缺陷。
连接到 FTP 服务器
FtpSession session = new FtpSession();
session.Server = "localhost";
session.Port = 21; //not required if using the default ftp port 21
session.Connect("anonymous", "someone@somewhere.com");
枚举子目录和文件
/*
* 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)
//dosomthing
更改当前目录
session.CurrentDirectory = somesubdirectory
上传文件
//Blocking call
session.CurrentDirectory.PutFile("somelocalfile");
//Async call using callback
session.CurrentDirectory.BeginPutFile("somelocalfile", +
new FtpAsyncDelegate(yourcallback);
//Async call using event
session.EndPutFile += new FtpFileEventHandler(yourhandler);
session.CurrectDirectory.BeginPutFile("somelocalfile");
//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.
*/
w.Close();
s.Close();
下载文件
//Blocking call
session.CurrentDirectory.GetFile("someremotefile");
//Async call using callback
session.CurrentDirectory.BeginGetFile("someremotefile", +
new FtpAsyncDelegate(yourcallback);
//Async call using event
session.EngGetFile += new FtpFileEventHandler(yourhandler);
session.CurrectDirectory.BeginGetFile("someremotefile");
//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.
*/
w.Close();
s.Close();
在 ftp 服务器上创建一个新文件
//method 1
session.CurrentDirectory.CreateFile(newfilename);
//method 2
Stream s = session.CurrentDirectory.CreateFileStream(newfilename);
// do something with s
s.Close();
从 ftp 服务器上删除文件
//method 1
session.CurrentDirectory.RemoveFile(remotefilename);
//method 2
IFtpItem item = session.CurrentDirectory.FindItem(remotefilename)
if(item != null)
session.CurrentDirectory.RemoveItem(item);
删除子目录
//method 1
session.CurrentDirectory.RemoveSubdir(subdirectoryname);
//method 2
IFtpItem item = session.CurrentDirectory.FindItem(sub directoryname)
if(item != null)
session.CurrentDirectory.RemoveItem(item);
刷新 ftp 目录的内容
/*
* 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
session.CurrentDirectory.Refresh();
重命名文件或目录
IFtpItem item = session.FindItem(directoryorfile);
if(item != null)
session.RenameSubitem(item, newname);
已知限制
- 不支持主动模式
- 不支持代理服务器
更新
- 删除了
FileCollection
和DirectoryCollection
- 添加了对 VB 的
for each
语句的支持 - VB 客户端现在可以直接使用
for each
语句访问FtpDirectory.Files
和FtpDirectory.SubDirectories
。
如果您发现任何错误,请给我发送电子邮件。请至少包含您发现的错误的描述。如果包含测试程序甚至解决方案,那就更好了。
玩得开心。