using FluentFTP; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.Authentication; using System.Text; using System.Threading.Tasks; namespace TestTool.RemoteTriggerAPP { internal class FluentFTPClient { #region 属性与构造函数 /// /// IP地址 /// public string IpAddr { get; set; } /// /// 相对路径 /// public string RelatePath { get; set; } /// /// 端口号 /// public int Port { get; set; } /// /// 用户名 /// public string UserName { get; set; } /// /// 密码 /// public string Password { get; set; } public FluentFTPClient(string ipAddr, int port, string userName, string password, string relatePath) { this.IpAddr = ipAddr; this.Port = port; this.UserName = userName; this.Password = password; this.RelatePath = relatePath; } #endregion #region 方法 public FtpListItem[] ListDir() { FtpListItem[] lists; using (var client = new FtpClient(this.IpAddr, this.UserName, this.Password, this.Port)) { client.Config.EncryptionMode = FtpEncryptionMode.Explicit; client.Config.SslProtocols = SslProtocols.Tls12; client.ValidateCertificate += Client_ValidateCertificate; client.Connect(); client.SetWorkingDirectory(this.RelatePath); lists = client.GetListing(); } return lists; } private void Client_ValidateCertificate(FluentFTP.Client.BaseClient.BaseFtpClient control, FtpSslValidationEventArgs e) { if (e.PolicyErrors != System.Net.Security.SslPolicyErrors.None) { e.Accept = false; } else { e.Accept = true; } } async internal Task UploadFileAsync(string localFilePath, string remoteFilePath) { bool isOk = false; using (var client = new AsyncFtpClient(this.IpAddr, this.UserName, this.Password, this.Port)) { client.Config.EncryptionMode = FtpEncryptionMode.Implicit; client.Config.SslProtocols = SslProtocols.Tls12; client.ValidateCertificate += Client_ValidateCertificate; await client.SetWorkingDirectory(this.RelatePath); await client.Connect(); var status = await client.UploadFile(localFilePath, "/" + remoteFilePath, FtpRemoteExists.Overwrite, false, FtpVerify.Retry); isOk = status == FtpStatus.Success; } return isOk; } public bool Upload(string dir, string file) { bool isOk = false; FileInfo fi = new FileInfo(Path.Combine(dir, file)); using (FileStream fs = fi.OpenRead()) { //long length = fs.Length; using (var client = new FtpClient(this.IpAddr, this.UserName, this.Password, this.Port)) { client.Config.EncryptionMode = FtpEncryptionMode.Implicit; client.Config.SslProtocols = SslProtocols.Tls12; client.ValidateCertificate += Client_ValidateCertificate; client.Connect(); client.SetWorkingDirectory(this.RelatePath); string remotePath = Path.GetFileName(file); FtpStatus status = client.UploadStream(fs, remotePath, FtpRemoteExists.Overwrite, true); isOk = status == FtpStatus.Success; } } return isOk; } /// /// 下载ftp /// /// /// /// public bool DownloadFile(string localAddress, string remoteAddress) { using (var client = new FtpClient(this.IpAddr, this.UserName, this.Password, this.Port)) { client.Config.EncryptionMode = FtpEncryptionMode.Explicit; client.Config.SslProtocols = SslProtocols.Tls12; client.ValidateCertificate += Client_ValidateCertificate; client.SetWorkingDirectory("/"); client.Connect(); if (client.DownloadFile(localAddress, remoteAddress) == FtpStatus.Success) { return true; } return false; } } #endregion } }