开发手册 欢迎您!
软件开发者资料库

.NET 使用FluentFTP实现FTP上传下载文件方法及示例代码

本文主要介绍.NET中使用FluentFTP实现向FTP Server服务器,上传下载的方法及使用示例代码,FluentFTP适用于.NET和.NET Standard的FTP和FTPS客户端,针对速度进行了优化。提供广泛的FTP命令,文件上传/下载,SSL / TLS连接,自动目录列表解析,文件哈希/校验和,文件权限/ CHMOD,FTP代理,UTF-8支持,支持Async/await等。完全用C#编写,没有外部依赖

1、FluentFTP安装引用

FluentFTP库使用Nuget安装引用

1) 使用Nuget命令安装

PM> Install-Package FluentFTP -Version 27.0.1

2) 使用Nuget管理工具搜索"FluentFTP"=>找到选择“安装”

相关文档VS(Visual Studio)中Nuget的使用

2、FluentFTP使用示例代码

// 创建 FTP clientFtpClient client = new FtpClient("123.123.123.123");// 如果您不指定登录凭证,我们将使用"anonymous"用户帐户client.Credentials = new NetworkCredential("david", "pass123");//开始连接Serverclient.Connect();//获取“/htdocs”文件夹中的文件和目录列表foreach (FtpListItem item in client.GetListing("/htdocs")) {//如果是 fileif (item.Type == FtpFileSystemObjectType.File){// get the file sizelong size = client.GetFileSize(item.FullName);}// 获取文件或文件夹的修改日期/时间DateTime time = client.GetModifiedTime(item.FullName);// 计算服务器端文件的哈希值(默认算法)FtpHash hash = client.GetHash(item.FullName);}//上传 fileclient.UploadFile(@"C:\MyVideo.mp4", "/htdocs/MyVideo.mp4");// 上传的文件重命名client.Rename("/htdocs/MyVideo.mp4", "/htdocs/MyVideo_2.mp4");// 下载文件client.DownloadFile(@"C:\MyVideo_2.mp4", "/htdocs/MyVideo_2.mp4");// 删除文件client.DeleteFile("/htdocs/MyVideo_2.mp4");// 递归删除文件夹client.DeleteDirectory("/htdocs/extras/");// 判断文件是否存在if (client.FileExists("/htdocs/big2.txt")){ }// 判断文件夹是否存在if (client.DirectoryExists("/htdocs/extras/")){ }//上传一个文件,重试3次才放弃client.RetryAttempts = 3;client.UploadFile(@"C:\MyVideo.mp4", "/htdocs/big.txt", FtpExists.Overwrite, false, FtpVerify.Retry);// 断开连接! good bye!client.Disconnect();

官方文档https://github.com/robinrodricks/FluentFTP