.NET 使用FluentFTP实现FTP上传下载文件方法及示例代码
时间:2022-10-21
1、FluentFTP安装引用
FluentFTP库使用Nuget安装引用
1) 使用Nuget命令安装
PM> Install-Package FluentFTP -Version 27.0.1
2) 使用Nuget管理工具搜索"FluentFTP"
=>找到选择“安装”
相关文档:VS(Visual Studio)中Nuget的使用
2、FluentFTP使用示例代码
// 创建 FTP client FtpClient client = new FtpClient("123.123.123.123"); // 如果您不指定登录凭证,我们将使用"anonymous"用户帐户 client.Credentials = new NetworkCredential("david", "pass123"); //开始连接Server client.Connect(); //获取“/htdocs”文件夹中的文件和目录列表 foreach (FtpListItem item in client.GetListing("/htdocs")) { //如果是 file if (item.Type == FtpFileSystemObjectType.File){ // get the file size long size = client.GetFileSize(item.FullName); } // 获取文件或文件夹的修改日期/时间 DateTime time = client.GetModifiedTime(item.FullName); // 计算服务器端文件的哈希值(默认算法) FtpHash hash = client.GetHash(item.FullName); } //上传 file client.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
特别声明:本站部分内容收集于互联网是出于更直观传递信息的目的。该内容版权归原作者所有,并不代表本站赞同其观点和对其真实性负责。如该内容涉及任何第三方合法权利,请及时与824310991@qq.com联系,我们会及时反馈并处理完毕。