using ExtendUI.FTPManager; using FTPop; using ryCommon; using ryCommonDb; 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; namespace SuperDesign.Manager.FTP { public partial class FrmUploadFilesToFtp : Form { public FrmUploadFilesToFtp() { InitializeComponent(); } public string EngName { get; set; } = ""; public int FTPId { get; set; } = 0; public string RemoteDir { get; set; } = ""; public List UploadInfos { get; set; } = new List(); private void BtnUpload_Click(object sender, EventArgs e) { Upload(); } private void Upload() { BtnUpload.Enabled = false; ProgTotal.Value = 0; ProgTotal.Maximum = UploadInfos.Count; new Thread(Start).Start(); void Start() { var isUpload_ok = 0; FTPUI ftp_ui = new FTPUI(new SQLiteDataProvider(), Application.StartupPath + "\\UserDb\\Ftp.dat"); for (int i = 0; i < UploadInfos.Count; i++) { if (IsExit) { break; } this.Invoke(new Action(() => { ProgTotal.Value = i; })); var upload=UploadInfos[i]; var ftp_info = ftp_ui.GetFTPInfo(upload.FtpName); if (ftp_info.Id == 0) { continue; } IFTP ftp = new FTPWinSCP(); ftp.FileTransferProgress += Session_FileTransferProgress; var opened = ftp.Open(ftp_info); if (opened == 1) { if (ftp.Upload(upload.FromPath, upload.RemotePath) != 1) { MessageBox.Show("本地目录:" + upload.FromPath + "\r\n远程目录:" + upload.RemotePath + "\r\n" + ftp.LastError, "出错", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { isUpload_ok = 1; } this.Invoke(new Action(() => { ProgTotal.Value = i+1; })); } else { MessageBox.Show("连接FTP出错=>" + ftp.LastError, "出错", MessageBoxButtons.OK, MessageBoxIcon.Error); } ftp.FileTransferProgress -= Session_FileTransferProgress; ftp.Close(); } this.Invoke(new Action(() => { BtnUpload.Enabled = true; ProgTotal.Value = UploadInfos.Count; if (isUpload_ok==1) { DialogResult = DialogResult.OK; } })); } } private void Session_FileTransferProgress(object sender, FTPop.FileTransferProgressEventArgs e) { if (IsExit) { e.Cancel = true; return; } this.Invoke(new Action(() => { progressBar1.Value = (e.FileProgress*100).ToInt(0, 100, 100); })); } private bool IsExit { get; set; } = false; private void FrmUploadFilesToFtp_FormClosing(object sender, FormClosingEventArgs e) { if (e.CloseReason == CloseReason.UserClosing && !BtnUpload.Enabled) { IsExit = true; e.Cancel = true; } } } public class UploadInfo { public string FromPath { get; set; } = ""; public string ToPath { get; set; } = ""; public string FtpName { get { return ToPath.GetStr("<",">"); } } public string RemotePath { get { var iPos = ToPath.IndexOf(">"); if(iPos>0) { return ToPath.Substring(iPos+1); } return ToPath; } } } }