1、FluentFTP类库简介
引用:FluentFTP是一款老外开发的基于.Net的支持FTP及的FTPS 的FTP类库,FluentFTP是完全托管的FTP客户端,被设计为易于使用和易于扩展。它支持文件和目录列表,上传和下载文件和SSL / TLS连接。它可以连接到Unix和Windows IIS建立FTP服务器。
2、简单使用
2.1、安装FluentFTP类库程序包
Install-Package FluentFTP -Version 33.0.3
更多版本:https://www.nuget.org/packages/FluentFTP/
2.2、代码:
使用的FtpClient构造函数:
using System;
using System.IO;
using FluentFTP;
namespace CallApp
{
class Program
{
static void Main(string[] args)
{
string host = "xxx";
string username = "xxx";
string password = "xxx";
string uploadFile = $@"E:\Works\Code\2020\Java\word\test.docx";
using FtpClient ftpClient = new FtpClient(host, username, password);
// 方法一:以文件流的方式上传文件
using FileStream fileStream = new FileStream(uploadFile, FileMode.Open);
// 参数一:文件流,参数二:ftp相对路径 + 文件名
ftpClient.Upload(fileStream, "creates/" + Path.GetFileName(uploadFile));
// 方法二:直接上传文件
// 参数一:本地文件路径,参数二:ftp相对路径 + 文件名
ftpClient.UploadFile(uploadFile, "creates/" + Path.GetFileName(uploadFile));
}
}
}