using FTPop; using GameBackup3H3.DbOp; using ryCommon; using SuperDesign.Manager.FTP; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using 开发辅助工具.Tools.SmartEditor; using static System.Net.WebRequestMethods; namespace 开发辅助工具.Manager.FTP { public partial class FrmUploadProg : Form { public FrmUploadProg() { InitializeComponent(); } public List UploadList { get; set; } = new List(); private bool IsExit = false; private void FrmTranProg_FormClosing(object sender, FormClosingEventArgs e) { if(e.CloseReason== CloseReason.UserClosing && progressBar1.Value!=100) { IsExit = true; e.Cancel = true; } } private Dictionary dict_ftp = new Dictionary(); public IFTP GetFTP(FTPInfo ftpinfo, out int result,out string errormsg) { errormsg = ""; var ftpid = ftpinfo.Id; if (dict_ftp.ContainsKey(ftpid)) { var ftp = dict_ftp[ftpid]; if (ftp.IsOpen) { result = 1; return ftp; } else { ftp.Close(); var ftp2 = new FTPop.FTPWinSCP(); ftp2.FileTransferProgress += Ftp_FileTransferProgress; result = ftp2.Open(ftpinfo); errormsg = ftp2.ErrorMsg; dict_ftp[ftpid] = ftp2; return ftp2; } } else { var ftp = new FTPop.FTPWinSCP(); ftp.FileTransferProgress += Ftp_FileTransferProgress; result = ftp.Open(ftpinfo); dict_ftp[ftpid] = ftp; errormsg = ftp.ErrorMsg; return ftp; } } private void FrmTranProg_Shown(object sender, EventArgs e) { ProgAll.Maximum = UploadList.Count; Thread th = new Thread(Start); th.Start(); void Start() { var error = 0; try { for (int i = 0; i < UploadList.Count; i++) { if (IsExit) { break; } var item = UploadList[i]; FTPop.FTPWinSCP ftp = (FTPop.FTPWinSCP)GetFTP(item.FtpInfo, out var result, out var err_msg); this.Invoke(new Action(() => { ProgAll.Value = i; label1.Text = "正在登录FTP..."; })); if (result == 1) { this.Invoke(new Action(() => { label1.Text = "正在上传文件..."; })); var remotedir = System.IO.Path.GetDirectoryName(item.RemotePath).Replace("\\", "/"); if (!ftp.FileExists(remotedir)) { ftp.CreateDir(remotedir); } else { if (item.LocalPath.Length > 0) { var remote_fileinfo = ftp.GetFileInfo(item.RemotePath); ryCommon.sType.FileTime local_dt = null; try { local_dt = RyFiles.GetFileDate(item.LocalPath); } catch { local_dt = new ryCommon.sType.FileTime(); MessageBox.Show("获取文件时间出错=>" + item.LocalPath, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } if (remote_fileinfo.LastWriteTime != DateTime.MinValue) { bool goto_next = false; if (remote_fileinfo.LastWriteTime.ToInt64() > local_dt.LastWriteTime.ToInt64()) { this.Invoke(new Action(() => { switch (MessageBox.Show("当前服务器上的文件日期比本地新,是否要替换?\r\n\r\n" + item.LocalPath, "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)) { case DialogResult.No: error++; goto_next = true; break; } })); } else if (local_dt.LastWriteTime.ToInt64() == remote_fileinfo.LastWriteTime.ToInt64()) { if (RyFiles.GetFileSize(item.LocalPath) != remote_fileinfo.Length) { this.Invoke(new Action(() => { switch (MessageBox.Show("当前本地文件与服务器文件日期一致,但大小不一致,是否要替换?\r\n\r\n本地:" + RyFiles.GetFileSizeStr(item.LocalPath) + "=>服务器:" + RyFiles.GetFileSizeStr(remote_fileinfo.Length), "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)) { case DialogResult.No: error++; goto_next = true; break; } })); } } if (goto_next) { continue; } } } } var LocalPath = item.LocalPath; var tmp_path = ""; if (LocalPath.Length == 0) { tmp_path = Application.StartupPath + "\\UserDb\\tmp\\FTP\\" + System.IO.Path.GetFileName(item.RemotePath); RyFiles.WriteAllText(tmp_path, item.Content, item.Encoding); LocalPath = tmp_path; } if (ftp.Upload(LocalPath, item.RemotePath) == 1) { ryCommon.sType.FileTime local_dt2 = null; try { local_dt2 = RyFiles.GetFileDate(LocalPath); } catch { local_dt2 = new ryCommon.sType.FileTime(); MessageBox.Show("获取文件时间出错2=>" + item.LocalPath, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } item.IsSuccess = true; item.FileWriteTime = local_dt2.LastWriteTime; item.FileSize = RyFiles.GetFileSize(LocalPath); this.Invoke(new Action(() => { FrmMainEditor.MainEditor.UpdateFTPFileState(item.FtpInfo.Id, item.RemotePath, local_dt2.LastWriteTime, item.FileSize); })); } else { this.Invoke(new Action(() => { MessageBox.Show("上传失败=>" + (item.LocalPath.Length > 0 ? item.LocalPath : item.RemotePath) + "\r\n" + ftp.ErrorMsg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); error++; })); if (ftp.ErrorMsg.IndexOfEx("already read to the end") >= 0) { ftp.Close(); //ftp= (FTPop.FTPWinSCP)FTPManger.GetFTP(item.FtpInfo, out var _); } } if (tmp_path.Length > 0) RyFiles.DeleteFile(tmp_path); } else { this.Invoke(new Action(() => { MessageBox.Show("FTP登录失败=>" + item.LocalPath + "\r\n" + err_msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); error++; })); } } foreach (var item in dict_ftp) { item.Value.FileTransferProgress -= Ftp_FileTransferProgress; item.Value.Close(); } } catch(Exception ex) { MessageBox.Show(ex.Message, "发生错误", MessageBoxButtons.OK, MessageBoxIcon.Information); } this.Invoke(new Action(() => { ProgAll.Value = ProgAll.Maximum; if (error == 0 && !IsExit) { DialogResult = DialogResult.OK; } else { DialogResult = DialogResult.No; } })); } } private DateTime dt_last = DateTime.MinValue; private void Ftp_FileTransferProgress(object sender, FTPop.FileTransferProgressEventArgs e) { if (IsExit) { e.Cancel = true; } this.Invoke(new Action(() => { try { if(dt_last /// 当LocalPath为空时生效 /// public string Content { get; set; } = ""; /// /// 当LocalPath为空时生效 /// public Encoding Encoding { get; set; } = Encoding.UTF8; /// /// 是否上传下载成功 /// public bool IsSuccess { get; set; } = false; /// /// 上传成功后获取文件最后修改时间 /// public DateTime FileWriteTime { get; set; } = DateTime.MinValue; /// /// 上传成功后返回文件大小 /// public long FileSize { get; set; } = 0; } }