最近需要重写一下一个C#客户端程序对FTP服务的支持,上网查了一些资料,看到了一个工具类edtFTPnet,于是今天下载了一个包了解了下。
网站首页、文档地址、下载地址如下:
1、网站首页:http://enterprisedt.com/products/edtftpnet/
2、文档地址:http://www.enterprisedt.com/products/edtftpnet/doc/manual/index.html
3、下载地址:http://enterprisedt.com/products/edtftpnet/editions/
在下载地址可下载三个版本,功能差异如下图所示,这里我选择Free版
下载到本地的包是一个zip压缩包,解压缩后目录结构如下:
E:\>tree edtftpnet-2.2.3
文件夹 PATH 列表
卷序列号为 002E0032 0E91:05C2
E:\EDTFTPNET-2.2.3
├─bin
├─doc
│ ├─images
│ └─manual
│ ├─api
│ │ ├─api
│ │ │ ├─fti
│ │ │ ├─html
│ │ │ ├─icons
│ │ │ ├─scripts
│ │ │ └─styles
│ │ │ └─Whidbey
│ │ ├─fti
│ │ ├─html
│ │ ├─icons
│ │ ├─scripts
│ │ └─styles
│ │ └─Whidbey
│ ├─html
│ ├─images
│ └─rfc
├─examples
│ ├─FTPClientCS
│ ├─FTPClientVB
│ ├─FTPConnectionCS
│ ├─FTPConnectionVB
│ └─Tutorial
│ └─images
├─lib
├─src
│ ├─config
│ ├─net
│ │ └─ftp
│ │ └─test
│ └─util
│ └─debug
└─test
├─conf
├─data
└─log
其中bin目录下文件如下:
E:\edtftpnet-2.2.3>tree /f bin
文件夹 PATH 列表
卷序列号为 002E0032 0E91:05C2
E:\EDTFTPNET-2.2.3\BIN
edtFTPnet.dll
test-edtFTPnet.dll
没有子文件夹
在.NET工程中手工添加引用edtFTPnet.dll,就可以使用edtFTPnet相关工具类了。
建立、关闭FTP连接代码如下:
FTPConnection ftp = new FTPConnection();
ftp.ServerAddress = "myservername";
ftp.UserName = "myusername";
ftp.Password = "mypassword";
ftp.Connect();
ftp.Close();
上传下载可采用下面三种方式进行:
Files (DownloadFile(String, String) and UploadFile(String, String)
Streams (DownloadStream(Stream, String) and UploadStream(Stream, String))
Byte-arrays (DownloadByteArray(String) and UploadByteArray(Byte[], String)
重命名、删除文件、获取文件大小:
Renaming files using the RenameFile(String, String) method.
Deleting files using the DeleteFile(String) method.
Getting a files size using the GetSize(String) method and its modification time using the GetLastWriteTime(String) method.
服务为每个会话维护了一个工作路径(working directory),当前工作路径可用WorkingDirectory属性设置。使用ChangeWorkingDirectory(String)方法可切换工作路径,使用ChangeWorkingDirectoryUp()方法可切换至父目录,使用DeleteDirectory(String)可删除目录,执行此方法前应保证目录中所有文件都被删除
---------------------------------------
下面是我写的一个DEMO程序。
我的操作系统版本为Win7旗舰版,.NET版本为4.5,VS版本为2012,服务端ServU版本为10.3.0.1。
第一步,使用ServU工具建立一个FTP服务,在地址192.168.13.98的计算机上建立一个名为flora的用户,密码为123456。
为测试代码方便,开通此用户的所有权限。
第二步,建立一个C#命令行应用程序,手动添加引用edtFTPnet.dll
第三步,输入C#代码如下:
using EnterpriseDT.Net.Ftp;
using EnterpriseDT.Util.Debug;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyFtpDemo
{
class Program
{
static void Main(string[] args)
{
//日志设置
FTPConnection.LogLevel = LogLevel.All;
FTPConnection.LogToConsole = false;
FTPConnection.LogToTrace = true;
FTPConnection.LogFile = "myftpdemo_log_20161017.log";
//创建FTP连接类
using (var ftpConn = new FTPConnection
{
//连接地址、端口号(默认为21)、用户名、密码
ServerAddress = "192.168.13.98",
ServerPort = 21,
UserName = "flora",
Password = "123456",
//编码方式一定要和服务器端保持一致,否则后面获取的文件名可能为乱码
ConnectMode = FTPConnectMode.PASV,
TransferType = FTPTransferType.ASCII,
DataEncoding = Encoding.GetEncoding("UTF-8"),
CommandEncoding = Encoding.GetEncoding("UTF-8")
})
{
//设置下载前后触发事件
ftpConn.TransferNotifyInterval = 1000;
ftpConn.Downloading += (sender, e) =>
{
Console.WriteLine("XXXXXXXX 下载开始 XXXXXXXX");
};
ftpConn.Downloaded += (sender, e) =>
{
Console.WriteLine("XXXXXXXX 下载完毕 XXXXXXXX");
};
try
{
//建立FTP连接
ftpConn.Connect();
Console.WriteLine("连接建立成功");
//获取文件列表
FTPFile[] ftpFiles = ftpConn.GetFileInfos();
foreach (FTPFile file in ftpFiles)
{
Console.WriteLine("---------");
Console.WriteLine("文件名:" + file.Name);
Console.WriteLine("是否为目录:" + file.Dir);
Console.WriteLine("文件大小:" + file.Size);
Console.WriteLine("最后修改时间:" + file.LastModified.ToString());
Console.WriteLine("---------");
}
//创建/删除目录
string tempDirectoryName = "TEMP-DIRECTORY";
if (ftpConn.DirectoryExists(tempDirectoryName))
{
ftpConn.DeleteDirectory(tempDirectoryName);
Console.WriteLine("已删除目录:" + tempDirectoryName);
}
ftpConn.CreateDirectory(tempDirectoryName);
Console.WriteLine("已创建目录:" + tempDirectoryName);
//创建/删除文件
string tempFileName = "TEMP-FILE.tmp";
if (ftpConn.Exists(tempFileName))
{
ftpConn.DeleteFile(tempFileName);
Console.WriteLine("已删除文件:" + tempFileName);
}
//上传文件
ftpConn.UploadFile("C:\\Users\\Tsybius\\Desktop\\haha.txt", "haha.txt");
Console.WriteLine("文件上传成功:" + "haha.txt");
//下载文件
if (ftpConn.Exists("haha.txt"))
{
ftpConn.DownloadFile("C:\\Users\\Tsybius\\Desktop\\hehe.txt", "haha.txt");
ftpConn.DeleteFile("haha.txt");
Console.WriteLine("已下载并删除文件:" + "haha.txt");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
//关闭FTP连接
ftpConn.Close();
}
}
Console.WriteLine("程序执行完毕");
Console.Read();
}
}
}
代码包含了如下几个功能:
- 设置日志(支持输出到文件、输出到控制台、输出到.NET Trace)
- 建立连接(需提供IP地址、用户名、密码)
- 编码设置
- 编写下载开始、完成时触发事件
- 创建、删除指定目录
- 创建、删除指定文件
- 上传文件到指定位置
- 从指定位置下载文件
需要注意的内容有:
- 编码设置一定要和服务器端保持一致,否则下载下来的文件很有可能是乱码
第四步,运行程序,运行结果如下:
程序记录日志(myftpdemo_log_20161017.log)如下:
INFO [FTPConnection] 17 十月 2016 17:40:30.891 : OS: 6.1.7601.65536, CLR: 4.0.30319.18444, DLL: 2.2.3.0
INFO [FTPConnection] 17 十月 2016 17:40:30.966 : Built: 11-Feb-2013 15:33:49 EST
INFO [FTPConnection] 17 十月 2016 17:40:30.990 : OS: 6.1.7601.65536, CLR: 4.0.30319.18444, DLL: 2.2.3.0
INFO [FTPConnection] 17 十月 2016 17:40:31.021 : Built: 11-Feb-2013 15:33:49 EST
DEBUG [FTPConnection] 17 十月 2016 17:40:31.057 : Set LocalDirectory='D:\MyPrograms\MyFtpDemo\MyFtpDemo\bin\Debug'
DEBUG [FTPClient] 17 十月 2016 17:40:31.087 : Connecting to 192.168.13.98:21
DEBUG [HostNameResolver] 17 十月 2016 17:40:31.173 : Resolving 192.168.13.98
DEBUG [HostNameResolver] 17 十月 2016 17:40:31.199 : 192.168.13.98 resolved to 192.168.13.98
INFO [BaseSocket] 17 十月 2016 17:40:31.244 : Connecting to 192.168.13.98:21 with timeout 120000 ms
DEBUG [BaseSocket] 17 十月 2016 17:40:31.385 : Successfully connected to 192.168.13.98:21
DEBUG [FTPControlSocket] 17 十月 2016 17:40:31.412 : Setting socket timeout=120000
INFO [FTPControlSocket] 17 十月 2016 17:40:31.462 : Command encoding=System.Text.UTF8Encoding
DEBUG [FTPControlSocket] 17 十月 2016 17:40:31.491 : StrictReturnCodes=False
DEBUG [FTPControlSocket] 17 十月 2016 17:40:31.543 : 220 Serv-U FTP Server v10.3 ready...
DEBUG [FTPConnection] 17 十月 2016 17:40:31.583 : Connected to 192.168.13.98 (instance=0)
DEBUG [FTPControlSocket] 17 十月 2016 17:40:31.612 : ---> USER flora
DEBUG [FTPControlSocket] 17 十月 2016 17:40:31.645 : 331 User name okay, need password.
DEBUG [FTPControlSocket] 17 十月 2016 17:40:31.669 : ---> PASS ********
DEBUG [FTPControlSocket] 17 十月 2016 17:40:31.704 : 230 User logged in, proceed.
DEBUG [FTPConnection] 17 十月 2016 17:40:31.730 : Successfully logged in
INFO [FTPConnection] 17 十月 2016 17:40:31.767 : Auto FEAT disabled
DEBUG [FTPControlSocket] 17 十月 2016 17:40:31.792 : ---> TYPE A
DEBUG [FTPControlSocket] 17 十月 2016 17:40:31.821 : 200 Type set to A.
DEBUG [FTPControlSocket] 17 十月 2016 17:40:31.847 : ---> PWD
DEBUG [FTPControlSocket] 17 十月 2016 17:40:31.875 : 257 "/" is current directory.
DEBUG [FTPConnection] 17 十月 2016 17:40:31.902 : GetFileInfos('')
DEBUG [FTPControlSocket] 17 十月 2016 17:40:31.931 : ---> SYST
DEBUG [FTPControlSocket] 17 十月 2016 17:40:31.956 : 215 UNIX Type: L8
DEBUG [FTPFileFactory] 17 十月 2016 17:40:31.978 : Selected UNIX parser
DEBUG [FTPControlSocket] 17 十月 2016 17:40:32.002 : ---> PWD
DEBUG [FTPControlSocket] 17 十月 2016 17:40:32.029 : 257 "/" is current directory.
DEBUG [FTPControlSocket] 17 十月 2016 17:40:32.059 : ---> PASV
DEBUG [FTPControlSocket] 17 十月 2016 17:40:32.083 : 227 Entering Passive Mode (192,168,13,98,28,95)
DEBUG [FTPControlSocket] 17 十月 2016 17:40:32.108 : Server supplied address=192.168.13.98
DEBUG [FTPControlSocket] 17 十月 2016 17:40:32.132 : Server supplied port=7263
DEBUG [FTPControlSocket] 17 十月 2016 17:40:32.150 : autoPassiveIPSubstitution=True
DEBUG [FTPControlSocket] 17 十月 2016 17:40:32.174 : remoteAddr=192.168.13.98
DEBUG [FTPControlSocket] 17 十月 2016 17:40:32.204 : Substituting server supplied IP (192.168.13.98) with remote host IP (192.168.13.98)
DEBUG [FTPControlSocket] 17 十月 2016 17:40:32.229 : NewPassiveDataSocket(192.168.13.98,7263)
INFO [BaseSocket] 17 十月 2016 17:40:32.271 : Connecting to 192.168.13.98:7263 with timeout 120000 ms
DEBUG [BaseSocket] 17 十月 2016 17:40:32.302 : Successfully connected to 192.168.13.98:7263
DEBUG [FTPControlSocket] 17 十月 2016 17:40:32.336 : Connected
DEBUG [FTPControlSocket] 17 十月 2016 17:40:32.360 : ---> LIST
DEBUG [FTPControlSocket] 17 十月 2016 17:40:32.393 : 150 Opening ASCII mode data connection for /bin/ls.
DEBUG [FTPClient] 17 十月 2016 17:40:32.415 : -->drwxrwxrwx 1 user group 0 Oct 17 17:31 TEMP-DIRECTORY
DEBUG [FTPClient] 17 十月 2016 17:40:32.437 : -->drwxrwxrwx 1 user group 0 Oct 17 17:23 测试目录
DEBUG [FTPClient] 17 十月 2016 17:40:32.464 : -->-rw-rw-rw- 1 user group 12 Oct 17 17:22 测试文件1.txt
DEBUG [FTPClient] 17 十月 2016 17:40:32.490 : -->-rw-rw-rw- 1 user group 22187 Oct 17 17:23 测试文件2.png
DEBUG [FTPClient] 17 十月 2016 17:40:32.516 : -->-rw-rw-rw- 1 user group 9741 Oct 17 17:23 测试文件3.xlsx
DEBUG [FTPControlSocket] 17 十月 2016 17:40:32.642 : 226 Transfer complete. 363 bytes transferred. 0.35 KB/sec.
DEBUG [FTPClient] 17 十月 2016 17:40:32.668 : Found 5 listing lines
DEBUG [FTPFileFactory] 17 十月 2016 17:40:32.712 : Parse() called using culture: Invariant Language (Invariant Country)
DEBUG [FTPFileFactory] 17 十月 2016 17:40:32.739 : Confirmed format UNIX
DEBUG [FTPConnection] 17 十月 2016 17:40:32.817 : DirectoryExists(TEMP-DIRECTORY)
DEBUG [FTPControlSocket] 17 十月 2016 17:40:32.843 : ---> CWD TEMP-DIRECTORY
DEBUG [FTPControlSocket] 17 十月 2016 17:40:32.871 : 250 Directory changed to /TEMP-DIRECTORY
DEBUG [FTPControlSocket] 17 十月 2016 17:40:32.899 : ---> CWD /
DEBUG [FTPControlSocket] 17 十月 2016 17:40:32.929 : 250 Directory changed to /
DEBUG [FTPControlSocket] 17 十月 2016 17:40:32.950 : ---> RMD TEMP-DIRECTORY
DEBUG [FTPControlSocket] 17 十月 2016 17:40:32.977 : 250 RMD command successful.
DEBUG [FTPControlSocket] 17 十月 2016 17:40:32.999 : ---> MKD TEMP-DIRECTORY
DEBUG [FTPControlSocket] 17 十月 2016 17:40:33.028 : 257 "/TEMP-DIRECTORY" directory created.
DEBUG [FTPConnection] 17 十月 2016 17:40:33.057 : Exists(TEMP-FILE.tmp)
DEBUG [FTPControlSocket] 17 十月 2016 17:40:33.080 : ---> SIZE TEMP-FILE.tmp
DEBUG [FTPControlSocket] 17 十月 2016 17:40:33.108 : 550 /TEMP-FILE.tmp: No such file.
DEBUG [FTPConnection] 17 十月 2016 17:40:33.132 : UploadFile(C:\Users\Tsybius\Desktop\haha.txt,haha.txt,False)
DEBUG [FTPConnection] 17 十月 2016 17:40:33.156 : Cancel resume
DEBUG [FTPControlSocket] 17 十月 2016 17:40:33.180 : ---> REST 0
DEBUG [FTPControlSocket] 17 十月 2016 17:40:33.220 : 350 Restarting at 0. Send STORE or RETRIEVE.
DEBUG [FTPControlSocket] 17 十月 2016 17:40:33.251 : ---> PASV
DEBUG [FTPControlSocket] 17 十月 2016 17:40:33.289 : 227 Entering Passive Mode (192,168,13,98,28,97)
DEBUG [FTPControlSocket] 17 十月 2016 17:40:33.311 : Server supplied address=192.168.13.98
DEBUG [FTPControlSocket] 17 十月 2016 17:40:33.336 : Server supplied port=7265
DEBUG [FTPControlSocket] 17 十月 2016 17:40:33.367 : autoPassiveIPSubstitution=True
DEBUG [FTPControlSocket] 17 十月 2016 17:40:33.390 : remoteAddr=192.168.13.98
DEBUG [FTPControlSocket] 17 十月 2016 17:40:33.423 : Substituting server supplied IP (192.168.13.98) with remote host IP (192.168.13.98)
DEBUG [FTPControlSocket] 17 十月 2016 17:40:33.448 : NewPassiveDataSocket(192.168.13.98,7265)
INFO [BaseSocket] 17 十月 2016 17:40:33.469 : Connecting to 192.168.13.98:7265 with timeout 120000 ms
DEBUG [BaseSocket] 17 十月 2016 17:40:33.504 : Successfully connected to 192.168.13.98:7265
DEBUG [FTPControlSocket] 17 十月 2016 17:40:33.525 : Connected
DEBUG [FTPControlSocket] 17 十月 2016 17:40:33.544 : ---> STOR haha.txt
DEBUG [FTPControlSocket] 17 十月 2016 17:40:33.580 : 150 Opening ASCII mode data connection for haha.txt.
DEBUG [FTPClient] 17 十月 2016 17:40:33.616 : Closing source stream
DEBUG [FTPControlSocket] 17 十月 2016 17:40:33.840 : 226 Transfer complete. 1,198 bytes transferred. 15.00 KB/sec.
DEBUG [FTPConnection] 17 十月 2016 17:40:33.864 : Exists(haha.txt)
DEBUG [FTPControlSocket] 17 十月 2016 17:40:33.884 : ---> SIZE haha.txt
DEBUG [FTPControlSocket] 17 十月 2016 17:40:33.916 : 213 1198
DEBUG [FTPConnection] 17 十月 2016 17:40:33.945 : DownloadFile(C:\Users\Tsybius\Desktop\hehe.txt,haha.txt)
DEBUG [FTPControlSocket] 17 十月 2016 17:40:33.967 : ---> SIZE haha.txt
DEBUG [FTPControlSocket] 17 十月 2016 17:40:34.004 : 213 1198
DEBUG [FTPConnection] 17 十月 2016 17:40:34.022 : GetLastWriteTime(haha.txt)
DEBUG [FTPControlSocket] 17 十月 2016 17:40:34.043 : ---> MDTM haha.txt
DEBUG [FTPControlSocket] 17 十月 2016 17:40:34.071 : 213 20161017094033.319
DEBUG [FTPControlSocket] 17 十月 2016 17:40:34.151 : ---> REST 0
DEBUG [FTPControlSocket] 17 十月 2016 17:40:34.176 : 350 Restarting at 0. Send STORE or RETRIEVE.
DEBUG [FTPControlSocket] 17 十月 2016 17:40:34.207 : ---> PASV
DEBUG [FTPControlSocket] 17 十月 2016 17:40:34.240 : 227 Entering Passive Mode (192,168,13,98,28,100)
DEBUG [FTPControlSocket] 17 十月 2016 17:40:34.265 : Server supplied address=192.168.13.98
DEBUG [FTPControlSocket] 17 十月 2016 17:40:34.299 : Server supplied port=7268
DEBUG [FTPControlSocket] 17 十月 2016 17:40:34.333 : autoPassiveIPSubstitution=True
DEBUG [FTPControlSocket] 17 十月 2016 17:40:34.356 : remoteAddr=192.168.13.98
DEBUG [FTPControlSocket] 17 十月 2016 17:40:34.375 : Substituting server supplied IP (192.168.13.98) with remote host IP (192.168.13.98)
DEBUG [FTPControlSocket] 17 十月 2016 17:40:34.393 : NewPassiveDataSocket(192.168.13.98,7268)
INFO [BaseSocket] 17 十月 2016 17:40:34.420 : Connecting to 192.168.13.98:7268 with timeout 120000 ms
DEBUG [BaseSocket] 17 十月 2016 17:40:34.453 : Successfully connected to 192.168.13.98:7268
DEBUG [FTPControlSocket] 17 十月 2016 17:40:34.477 : Connected
DEBUG [FTPControlSocket] 17 十月 2016 17:40:34.509 : ---> RETR haha.txt
DEBUG [FTPControlSocket] 17 十月 2016 17:40:34.568 : 150 Opening ASCII mode data connection for haha.txt (1198 Bytes).
DEBUG [FTPControlSocket] 17 十月 2016 17:40:34.841 : 226 Transfer complete. 1,198 bytes transferred. 1.17 KB/sec.
DEBUG [FTPConnection] 17 十月 2016 17:40:34.879 : DeleteFile(haha.txt)
DEBUG [FTPConnection] 17 十月 2016 17:40:34.908 : GetLastWriteTime(haha.txt)
DEBUG [FTPControlSocket] 17 十月 2016 17:40:34.937 : ---> MDTM haha.txt
DEBUG [FTPControlSocket] 17 十月 2016 17:40:34.963 : 213 20161017094033.319
DEBUG [FTPControlSocket] 17 十月 2016 17:40:34.985 : ---> DELE haha.txt
DEBUG [FTPControlSocket] 17 十月 2016 17:40:35.015 : 250 DELE command successful.
DEBUG [FTPConnection] 17 十月 2016 17:40:35.039 : Closing connection (instance=0)
DEBUG [FTPFileFactory] 17 十月 2016 17:40:35.066 : Defaulting to Unix parsing
DEBUG [FTPControlSocket] 17 十月 2016 17:40:35.093 : ---> QUIT
DEBUG [FTPControlSocket] 17 十月 2016 17:40:35.123 : 221 Goodbye, closing session.
END