SuperDesign/Source/开发辅助工具/Manager/FTP/FTP_WinSCP.cs
zilinsoft 993f1ca1a9 ### 2024-12-20 星期五更新
------
#### SuperDesign    V3.0.2412.2001
- *.[新增]新增程序更新日志设置和自动发布功能。
- *.[修复]修复Post数据格式不正确时双击文本框会导致软件闪退的BUG。
2024-12-20 08:15:19 +08:00

330 lines
11 KiB
C#

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
{
/// <summary>
/// FTP管理
/// </summary>
public class FTPWinSCP : IFTP, IDisposable
{
private bool disposed = false;
readonly WinSCP.Session session = new WinSCP.Session();
public string CurRemoteFolder { get; private set; } = "";
/// <summary>
/// 当前ID
/// </summary>
public int Id { get; set; } = 0;
private FTPInfo _ftpinfo = null;
/// <summary>
/// 打开FTP
/// </summary>
/// <param name="ftpinfo"></param>
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; }
}
/// <summary>
/// 最后一次错误
/// </summary>
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; }
}
/// <summary>
/// 关闭FTP
/// </summary>
/// <returns></returns>
public int Close()
{
if (session.Opened)
{ session.Close(); }
return 1;
}
/// <summary>
/// 移除文件
/// </summary>
/// <param name="remotePath"></param>
/// <returns></returns>
public int RemoveFiles(string remotePath)
{
var result= session.RemoveFiles(remotePath);
return result.IsSuccess?1:-1;
}
/// <summary>
/// 移动文件
/// </summary>
/// <param name="fromPath"></param>
/// <param name="toPath"></param>
/// <returns></returns>
public int MoveFile(string fromPath, string toPath)
{
session.MoveFile(fromPath, toPath);
return 1;
}
/// <summary>
///
/// </summary>
public object Tag { get; set; } = null;
/// <summary>
/// 文件是否存在
/// </summary>
/// <param name="remotePath"></param>
/// <returns></returns>
public bool FileExists(string remotePath)
{
return session.FileExists(remotePath);
}
/// <summary>
/// 文件大小
/// </summary>
/// <param name="remotePath"></param>
/// <returns></returns>
public Int64 FileSize(string remotePath)
{
try
{
return session.GetFileInfo(remotePath).Length;
}
catch { return -1; }
}
/// <summary>
/// 创建文件夹
/// </summary>
/// <param name="remotePath"></param>
/// <returns></returns>
public bool CreateDir(string remotePath)
{
try
{
session.CreateDirectory(remotePath);
return FileExists(remotePath);
}
catch { return false; }
}
/// <summary>
/// 文件最后修改时间
/// </summary>
/// <param name="remotePath"></param>
/// <returns></returns>
public DateTime FileLastWriteTime(string remotePath)
{
try
{
return session.GetFileInfo(remotePath).LastWriteTime;
}
catch(Exception ex) { LastError = ex.Message; return new DateTime(2000,1,1); }
}
/// <summary>
/// 文件信息
/// </summary>
/// <param name="remotePath"></param>
/// <returns></returns>
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(); }
}
/// <summary>
/// 显示文件夹列表
/// </summary>
/// <param name="CurFolder"></param>
/// <returns></returns>
public List<ExtendUI.FTPManager.RemoteFileInfo> ListDirectory(string CurFolder)
{
var list = new List<ExtendUI.FTPManager.RemoteFileInfo>();
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;
}
/// <summary>
/// 上传
/// </summary>
/// <param name="localPath"></param>
/// <param name="remotePath"></param>
/// <returns></returns>
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;
}
}
/// <summary>
/// 文件传输事件
/// </summary>
public event FileTransferProgressEventHandler FileTransferProgress;
~FTPWinSCP()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// 下载
/// </summary>
/// <param name="remotePath"></param>
/// <param name="localPath"></param>
/// <returns></returns>
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;
}
}
}