FTP 客户端,基于 BCL TcpClient + SslStream 从头实现。 模仿 HttpClient 异步设计模式。 注意: 此类非线程安全,请勿从多线程同时操作同一实例。
FtpClient
01 Syntax
Microsoft.VisualBasic.Net.FTP.FtpClient
02 Methods
| Name | Overloads | Summary |
|---|---|---|
| .ctor | 1 | 创建 FTP 客户端实例。 |
| ConnectAsync | 1 | 显式连接到 FTP 服务器并完成认证。 通常无需调用——首次操作时会自动连接。 |
| Disconnect | 1 | 断开连接。可再次调用 ConnectAsync 重连。 |
| DownloadAsync | 1 | 下载远程文件到指定流。 |
| DownloadFileAsync | 1 | 下载远程文件到本地路径。 |
| GetFileSizeAsync | 1 | 获取远程文件大小 (字节)。服务器不支持 SIZE 时抛异常。 |
| FileExistsAsync | 1 | 检查远程文件是否存在。 |
| GetFileInfoAsync | 1 | 获取远程文件信息 (大小 + 修改时间)。 |
| SetWorkingDirectoryAsync | 1 | 更改远程工作目录 (CWD)。 |
| PrintWorkingDirectoryAsync | 1 | 获取当前工作目录 (PWD)。 |
| ListDirectoryAsync | 1 | 列出远程目录内容 (NLST,仅返回名称)。目录不存在或无法访问时抛出异常。 |
| ParsePasvResponse | 1 | 解析 PASV 响应: "Entering Passive Mode (h1,h2,h3,h4,p1,p2)" |
| ParseEpsvResponse | 1 | 解析 EPSV 响应: "Entering Extended Passive Mode (|||port|)" |
| ParseMdtmDate | 1 | 解析 MDTM 日期: "20230817123045" → DateTime (UTC) |
| Dispose | 1 |
03 Properties
| Name | Overloads | Summary |
|---|---|---|
| Credentials | 1 | 登录凭据。必须在连接前设置。 |
| Host | 1 | |
| Port | 1 | |
| IsConnected | 1 | |
| IsAuthenticated | 1 | |
| IsSecureConnection | 1 |
04 Members
创建 FTP 客户端实例。
Parameters
| Name | Type | Description |
|---|---|---|
host | String | FTP 服务器主机名或 IP |
port | Int32 | 端口,默认 21 (隐式 SSL 时默认 990) |
options | FtpClientOptions | 配置选项,为 Nothing 时使用默认值 |
credentials | FtpCredentials | 登录凭据,为 Nothing 时使用匿名登录 |
ConnectAsync(
CancellationToken)显式连接到 FTP 服务器并完成认证。 通常无需调用——首次操作时会自动连接。
Disconnect
断开连接。可再次调用 ConnectAsync 重连。
DownloadAsync(
String, Stream, CancellationToken, IProgress(Of FtpDownloadProgress))下载远程文件到指定流。
Parameters
| Name | Type | Description |
|---|---|---|
remotePath | String | 远程文件路径 |
destination | Stream | 目标流 |
ct | CancellationToken | 取消令牌 |
progress | IProgress(Of FtpDownloadProgress) | 进度回调 (可为 Nothing) |
DownloadFileAsync(
String, String, Boolean, CancellationToken, IProgress(Of FtpDownloadProgress))下载远程文件到本地路径。
Parameters
| Name | Type | Description |
|---|---|---|
remotePath | String | 远程文件路径 |
localPath | String | 本地保存路径 |
overwrite | Boolean | 是否覆盖已有文件,默认 True |
ct | CancellationToken | 取消令牌 |
progress | IProgress(Of FtpDownloadProgress) | 进度回调 (可为 Nothing) |
GetFileSizeAsync(
String, CancellationToken)获取远程文件大小 (字节)。服务器不支持 SIZE 时抛异常。
FileExistsAsync(
String, CancellationToken)检查远程文件是否存在。
GetFileInfoAsync(
String, CancellationToken)获取远程文件信息 (大小 + 修改时间)。
SetWorkingDirectoryAsync(
String, CancellationToken)更改远程工作目录 (CWD)。
PrintWorkingDirectoryAsync(
CancellationToken)获取当前工作目录 (PWD)。
ListDirectoryAsync(
String, CancellationToken)列出远程目录内容 (NLST,仅返回名称)。目录不存在或无法访问时抛出异常。
Parameters
| Name | Type | Description |
|---|---|---|
remotePath | String | 远程目录路径,空或 Nothing 表示当前工作目录。 |
ct | CancellationToken | 取消令牌 |
Returns
目录项名称数组(不含路径前缀)。
ParsePasvResponse(
String)解析 PASV 响应: "Entering Passive Mode (h1,h2,h3,h4,p1,p2)"
ParseEpsvResponse(
String)解析 EPSV 响应: "Entering Extended Passive Mode (|||port|)"
ParseMdtmDate(
String)解析 MDTM 日期: "20230817123045" → DateTime (UTC)
Credentials
登录凭据。必须在连接前设置。
Host
Port
IsConnected
IsAuthenticated
IsSecureConnection
Dispose()