C# FluentFTP
最后修改于 2023 年 7 月 5 日
在本文中,我们将展示如何在 C# 中使用 FluentFTP 处理 FTP。
FluentFTP 是一个用于 .NET 的 FTP 和 FTPS 客户端。 它支持许多 FTP 命令,文件上传和下载,SSL / TLS 连接,文件哈希和校验和,文件权限或 FTP 代理。
文件传输协议 (FTP) 是一种标准的网络协议,用于在计算机网络上的客户端和服务器之间传输计算机文件。 客户端和服务器使用一组 FTP 命令进行通信,例如 DELE、RETR 或 CWD。
$ dotnet add package FluentFTP
我们将包添加到项目中。
C# FluentFTP 创建目录
在第一个例子中,我们创建一个远程目录。
using FluentFTP;
var host = "example.com";
var username = "user7";
var passwd = "s$cret";
var path = "/web/test/";
using var con = new FtpClient(host, username, passwd);
con.Connect();
var ok = con.CreateDirectory(path);
if (ok) {
Console.WriteLine("directory successfully created");
} else {
Console.WriteLine("failed to create directory");
}
该程序在提供的 FTP 服务器上创建一个新目录,并打印一条消息。
var path = "/web/test/";
这是新目录的名称。
using var con = new FtpClient(host, username, passwd); con.Connect();
我们创建一个新的 FTP 客户端并建立连接。
var ok = con.CreateDirectory(path);
使用 CreateDirectory 创建一个新目录。 该方法返回一个布尔值,指示创建是否成功。
if (ok) {
Console.WriteLine("directory successfully created");
} else {
Console.WriteLine("failed to create directory");
}
根据返回的值,我们在控制台中打印一条消息。
C# FluentFTP 下载文件
使用 DownloadFile 方法下载文件。
using FluentFTP;
var host = "example.com";
var username = "user7";
var passwd = "s$cret";
var path = "/web/csharp/quest-pdf/index.html";
var baseName = Path.GetFileName(path);
using var con = new FtpClient(host, username, passwd);
con.Connect();
var status = con.DownloadFile($"{baseName}", path,
FtpLocalExists.Overwrite, FtpVerify.Retry);
var msg = status switch {
FtpStatus.Success => "file successfully downloaded",
FtpStatus.Failed => "failed to download file",
_ => "unknown"
};
Console.WriteLine(msg);
该程序从 FTP 服务器下载一个 index.html 文件。 它打印一条关于结果的消息。
var status = con.DownloadFile($"{baseName}", path,
FtpLocalExists.Overwrite, FtpVerify.Retry);
我们使用 DownloadFile 下载文件。 第一个参数是本地路径名,第二个参数是远程路径名。 如果磁盘上存在本地文件,则 FtpLocalExists.Overwrite 选项会覆盖本地文件。 使用 FtpVerify.Retry 选项,我们告诉客户端,如果下载失败,则重试几次下载。 该方法返回 FtpStatus。
var msg = status switch {
FtpStatus.Success => "file successfully downloaded",
FtpStatus.Failed => "failed to download file",
_ => "unknown"
};
根据返回的状态值,我们提供一条消息。
C# FluentFTP 上传文件
使用 UploadFile 方法上传文件。
using FluentFTP;
var host = "example.com";
var username = "user7";
var passwd = "s$cret";
var rpath = "/web/test/index.html";
var lpath = "index.html";
using var con = new FtpClient(host, username, passwd);
con.Connect();
var status = con.UploadFile(lpath, rpath, FtpRemoteExists.Overwrite,
true, FtpVerify.Retry);
var msg = status switch
{
FtpStatus.Success => "file successfully uploaded",
FtpStatus.Failed => "failed to upload file",
_ => "unknown"
};
Console.WriteLine(msg);
该程序上传一个文件并打印一条消息。
var status = con.UploadFile(lpath, rpath, FtpRemoteExists.Overwrite,
true, FtpVerify.Retry);
我们使用 UploadFile 将文件上传到 FTP 服务器。 如果远程文件已存在,则将其覆盖。 如果上传失败,我们会重试几次上传。
var msg = status switch
{
FtpStatus.Success => "file successfully uploaded",
FtpStatus.Failed => "failed to upload file",
_ => "unknown"
};
根据返回的状态值,我们在终端中打印一条消息。
C# FluentFTP 列出目录
下一个示例列出给定远程目录的内容。
using FluentFTP;
var host = "example.com";
var username = "user7";
var passwd = "s$cret";
var path = "/";
using var con = new FtpClient(host, username, passwd);
con.Connect();
var items = con.GetListing(path);
foreach (FtpListItem item in items) {
if (item.Type == FtpObjectType.File) {
Console.WriteLine($"f {item.Name}");
} else if (item.Type == FtpObjectType.Directory) {
Console.WriteLine($"d {item.Name}");
} else {
Console.WriteLine($"{item.Name}");
}
}
该程序列出根目录的内容。 在列表中,我们指定该项目是文件还是目录。
var items = con.GetListing(path);
GetListing 方法从服务器返回文件列表。 返回的每个 FtpListItem 对象都包含有关该项目的信息。
foreach (FtpListItem item in items) {
if (item.Type == FtpObjectType.File) {
Console.WriteLine($"f {item.Name}");
} else if (item.Type == FtpObjectType.Directory) {
Console.WriteLine($"d {item.Name}");
} else {
Console.WriteLine($"{item.Name}");
}
}
我们遍历 FTP 列表项的数组,并将每个名称打印到目录。 我们为文件附加一个 f 字符,为目录附加一个 d 字符。
来源
在本文中,我们介绍了 FluentFTP 库。 我们展示了如何创建目录、下载和上传文件以及列出目录内容。
作者
列出所有 C# 教程。