using ExtendUI.FTPManager; using ryCommon; using System; using System.Collections.Generic; using System.Linq; using System.Text; using WinSCP; using static System.Collections.Specialized.BitVector32; namespace FTPop { /// /// FTP管理 /// public class FTPWinSCP : IFTP, IDisposable { private bool disposed = false; readonly WinSCP.Session session = new WinSCP.Session(); public string CurRemoteFolder { get; private set; } = ""; /// /// 当前ID /// public int Id { get; set; } = 0; private FTPInfo _ftpinfo = null; /// /// 打开FTP /// /// public int Open(FTPInfo ftpinfo) { _ftpinfo= ftpinfo; if (ftpinfo==null) { return -1; } try { SessionOptions sessionOptions = new SessionOptions { Protocol = Protocol.Ftp, HostName = ftpinfo.IP, PortNumber = ftpinfo.Port, UserName = ftpinfo.UserName, Password = ftpinfo.Pwd }; if (ftpinfo.Encrypt == 0) { sessionOptions.FtpSecure = FtpSecure.None; } else if (ftpinfo.Encrypt == 1) { sessionOptions.FtpSecure = FtpSecure.Implicit; } else { sessionOptions.FtpSecure = FtpSecure.Explicit; } //sessionOptions. if (ftpinfo.Encrypt != 0) { sessionOptions.GiveUpSecurityAndAcceptAnyTlsHostCertificate = true; } if (FileTransferProgress != null) { session.FileTransferProgress += Session_FileTransferProgress; ; } session.Open(sessionOptions); if (session.Opened) { return 1; } else { return -1; } } catch(Exception ex) { LastError = ex.Message; return -1; } } /// /// 最后一次错误 /// public string LastError { get; set; } = ""; private void Session_FileTransferProgress(object sender, WinSCP.FileTransferProgressEventArgs e) { var progress = new FileTransferProgressEventArgs() { Cancel = e.Cancel, LocalPath = e.FileName, RemotePath = e.FileName, FileProgress = e.FileProgress, IsComplete = e.FileProgress == 1, FTPInfo = _ftpinfo, Speed = e.CPS }; FileTransferProgress?.Invoke(this, progress); if (progress.Cancel) { e.Cancel = progress.Cancel; } } /// /// 关闭FTP /// /// public int Close() { if (session.Opened) { session.Close(); } return 1; } /// /// 移除文件 /// /// /// public int RemoveFiles(string remotePath) { var result= session.RemoveFiles(remotePath); return result.IsSuccess?1:-1; } /// /// 移动文件 /// /// /// /// public int MoveFile(string fromPath, string toPath) { session.MoveFile(fromPath, toPath); return 1; } /// /// /// public object Tag { get; set; } = null; /// /// 文件是否存在 /// /// /// public bool FileExists(string remotePath) { return session.FileExists(remotePath); } /// /// 文件大小 /// /// /// public Int64 FileSize(string remotePath) { try { return session.GetFileInfo(remotePath).Length; } catch { return -1; } } /// /// 创建文件夹 /// /// /// public bool CreateDir(string remotePath) { try { session.CreateDirectory(remotePath); return FileExists(remotePath); } catch { return false; } } /// /// 文件最后修改时间 /// /// /// public DateTime FileLastWriteTime(string remotePath) { try { return session.GetFileInfo(remotePath).LastWriteTime; } catch(Exception ex) { LastError = ex.Message; return new DateTime(2000,1,1); } } /// /// 文件信息 /// /// /// public ExtendUI.FTPManager.RemoteFileInfo GetFileInfo(string remotePath) { try { var info = session.GetFileInfo(remotePath); return new ExtendUI.FTPManager.RemoteFileInfo() { LastWriteTime = info.LastWriteTime, Length = info.Length, Name = info.Name, IsDirectory = info.IsDirectory }; } catch (Exception ex) { LastError = ex.Message; return new ExtendUI.FTPManager.RemoteFileInfo(); } } /// /// 显示文件夹列表 /// /// /// public List ListDirectory(string CurFolder) { var list = new List(); var result = session.ListDirectory(CurFolder); for (int i = 0; i < result.Files.Count; i++) { var file = result.Files[i]; list.Add(new ExtendUI.FTPManager.RemoteFileInfo() { FullName=file.FullName, IsDirectory=file.IsDirectory, Name=file.Name, Length=file.Length, LastWriteTime=file.LastWriteTime}); } return list; } /// /// 上传 /// /// /// /// public int Upload(string localPath, string remotePath) { try { var progress = new FileTransferProgressEventArgs() { LocalPath = localPath, RemotePath= remotePath, FileProgress = 0, IsStart=true, FTPInfo=_ftpinfo }; FileTransferProgress?.Invoke(this, progress); TransferOptions transferOptions = new TransferOptions { TransferMode = TransferMode.Binary, OverwriteMode= OverwriteMode.Overwrite }; TransferOperationResult transferResult; transferResult = session.PutFiles(localPath, remotePath, false, transferOptions); //Throw on any error transferResult.Check(); return transferResult.IsSuccess ? 1 : -1; } catch(Exception ex) { LastError = ex.Message; return -2; } } /// /// 文件传输事件 /// public event FileTransferProgressEventHandler FileTransferProgress; ~FTPWinSCP() { Dispose(false); } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } /// /// 下载 /// /// /// /// public int Download(string remotePath, string localPath) { if (!RyFiles.FileOrDirExist(System.IO.Path.GetDirectoryName(localPath))) { RyFiles.CreateDirectory(System.IO.Path.GetDirectoryName(localPath)); } try { TransferOptions transferOptions = new TransferOptions { TransferMode = TransferMode.Binary, OverwriteMode = OverwriteMode.Overwrite }; TransferOperationResult transferResult; transferResult = session.GetFiles(remotePath, localPath, false, transferOptions); //Throw on any error transferResult.Check(); if (transferResult.IsSuccess) { if (FileSize(remotePath) == RyFiles.GetFileSize(localPath)) { return 1; } else { ErrorMsg = "文件不完整。"; return -3; } } else { return -2; } } catch (Exception ex) { ErrorMsg = ex.Message; return -1; } } public string ErrorMsg { get; set; } = ""; protected virtual void Dispose(bool disposing) { if (!this.disposed) { Close(); // If disposing equals true, dispose all managed // and unmanaged resources. if (disposing) { // Dispose managed resources. session.Dispose(); } // Release unmanaged resources. If disposing is false, // only the following code is executed. // Note that this is not thread safe. // Another thread could start disposing the object // after the managed resources are disposed, // but before the disposed flag is set to true. // If thread safety is necessary, it must be // implemented by the client. } disposed = true; } } }