XYS.Utils.FTP
2.3.1
.NET 8.0
This package targets .NET 8.0. The package is compatible with this framework or higher.
.NET Core 3.1
This package targets .NET Core 3.1. The package is compatible with this framework or higher.
.NET Standard 2.0
This package targets .NET Standard 2.0. The package is compatible with this framework or higher.
.NET Framework 4.6.2
This package targets .NET Framework 4.6.2. The package is compatible with this framework or higher.
dotnet add package XYS.Utils.FTP --version 2.3.1
NuGet\Install-Package XYS.Utils.FTP -Version 2.3.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="XYS.Utils.FTP" Version="2.3.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="XYS.Utils.FTP" Version="2.3.1" />
<PackageReference Include="XYS.Utils.FTP" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add XYS.Utils.FTP --version 2.3.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: XYS.Utils.FTP, 2.3.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package XYS.Utils.FTP@2.3.1
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=XYS.Utils.FTP&version=2.3.1
#tool nuget:?package=XYS.Utils.FTP&version=2.3.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
XYS.Utils.FTP
通用、异步、高并发的 FTP 工具库,基于 FluentFTP 封装。
特性
FtpAgent门面:上传/下载(流与本地文件)、列目录、存在判断、删除、建目录、重命名。- 全部操作异步优先(支持
CancellationToken),另提供同步包装。 - 内置连接池(
FtpConnectionPool)复用AsyncFtpClient,MaxConnections限并发,空闲超时自动回收。 - FluentFTP 依赖收敛到唯一适配器
FluentFtpConnection;核心逻辑面向IFtpConnection抽象,便于替换或单测。 - 支持 net8.0 / net462 / netcoreapp3.1 / netstandard2.0 / netstandard2.1。
快速开始
// 方式一:显式配置
using var agent = new FtpAgent(new FtpOptions
{
Host = "10.0.0.1", Port = 21, User = "u", Password = "p",
MaxConnections = 10,
});
await agent.UploadAsync(stream, "/uploads/data.txt");
// 方式二:从 xys.config.json 读取
using var agent2 = FtpAgent.FromConfig("default");
FtpAgent 实现 IDisposable,Dispose 时会释放内部连接池及所有空闲连接。
同步 API
每个异步操作都有一个对应的同步包装(去掉方法名末尾的 Async),内部走 Task.Run(...).GetAwaiter().GetResult(),可安全在任意上下文(含 UI 线程、老 ASP.NET 同步动作)调用,代价是每次多一次线程切换。异步优先,仅在无法改造同步调用链时使用。
using var agent = new FtpAgent(options);
agent.Upload(stream, "/uploads/data.txt"); // 同步等价:UploadAsync
bool exists = agent.FileExists("/uploads/data.txt");
操作清单
| 异步 | 同步 | 说明 |
|---|---|---|
UploadAsync(Stream, remotePath, overwrite, createDir, ct) |
Upload(...) |
上传流;stream 可 Seek 时其 Position 会被重置到 0 |
UploadFileAsync(localPath, remotePath, overwrite, createDir, ct) |
UploadFile(...) |
上传本地文件 |
DownloadAsync(Stream, remotePath, ct) |
Download(...) |
下载到流,从流当前 Position 开始写入 |
DownloadFileAsync(localPath, remotePath, overwrite, ct) |
DownloadFile(...) |
下载到本地文件 |
ListAsync(remoteDir, ct) |
List(...) |
列目录,返回 IReadOnlyList<FtpEntry> |
FileExistsAsync(remotePath, ct) |
FileExists(...) |
文件是否存在 |
DirectoryExistsAsync(remoteDir, ct) |
DirectoryExists(...) |
目录是否存在 |
DeleteFileAsync(remotePath, ct) |
DeleteFile(...) |
删除文件 |
DeleteDirectoryAsync(remoteDir, ct) |
DeleteDirectory(...) |
删除目录 |
CreateDirectoryAsync(remoteDir, ct) |
CreateDirectory(...) |
创建目录 |
RenameAsync(fromPath, toPath, ct) |
Rename(...) |
重命名/移动 |
错误处理
- 业务失败(如上传返回
FtpStatus != Success、创建目录失败)抛FtpOperationException,异常发生时不销毁连接,仍可复用。 - 连接级异常(网络断开、TLS 握手失败、
IOException等)由底层抛出并向上冒泡;租约会被标记失败,归还时连接被Dispose,不回池,下次借出会重建。 - 取消
CancellationToken会中断当前操作,正在执行中的连接同样会被标记失败并销毁。
try
{
await agent.UploadAsync(stream, "/a/b.txt", ct: token);
}
catch (FtpOperationException ex) // 业务失败:路径不存在、权限拒绝等
{
logger.Warn(ex.Message);
}
catch (OperationCanceledException) // 取消
{
...
}
catch (Exception ex) // 网络/IO 异常
{
logger.Error(ex, "FTP 操作失败");
throw;
}
连接池调优
| 参数 | 默认 | 建议 |
|---|---|---|
MaxConnections |
10 | 并发上限;设过大服务器可能拒绝,设过小请求排队 |
IdleTimeout |
60000 ms | 空闲连接可存活时长;较长空闲后借出会强制重建 |
ConnectTimeout |
15000 ms | 单次 AutoConnect 超时 |
UseSsl |
false | 开启后 FluentFTP EncryptionMode.Auto + 接受任意证书 |
线程/请求超过 MaxConnections 时 BorrowAsync 会异步等待可用许可,不阻塞线程;等待可用 CancellationToken 取消。
配置(xys.config.json)
"ftp": {
"default": {
"host": "10.0.0.1",
"port": 21,
"user": "u",
"password": "p",
"useSsl": false,
"connectTimeout": 15000,
"maxConnections": 10,
"idleTimeout": 60000
}
}
可命名多个节点(例如 ftp:archive、ftp:report),通过 FtpAgent.FromConfig("archive") 分别构造。仓库中的 xys.config.json 不应写入明文凭据,仅在部署环境的实际配置文件中填写。
架构
FtpAgent ──── 面向调用方,异步 + 同步包装 + 错误分类
│
▼
FtpConnectionPool ── SemaphoreSlim 限并发 + ConcurrentBag 空闲桶 + 空闲超时回收
│
▼
IFtpConnection ── 瘦抽象(13 成员),可被 FakeFtpConnection 替代做单测
│
▼
FluentFtpConnection ── 唯一直接依赖 FluentFTP 的类型
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 is compatible. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
| .NET Framework | net461 was computed. net462 is compatible. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETCoreApp 3.1
- FluentFTP (>= 54.2.0)
- XYS.Utils.Sys (>= 2.3.1)
-
.NETFramework 4.6.2
- FluentFTP (>= 54.2.0)
- XYS.Utils.Sys (>= 2.3.1)
-
.NETStandard 2.0
- FluentFTP (>= 54.2.0)
- XYS.Utils.Sys (>= 2.3.1)
-
.NETStandard 2.1
- FluentFTP (>= 54.2.0)
- XYS.Utils.Sys (>= 2.3.1)
-
net8.0
- FluentFTP (>= 54.2.0)
- XYS.Utils.Sys (>= 2.3.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 2.3.1 | 112 | 7/15/2026 |