private void timer1_Tick(object sender, EventArgs e) { try { timer1.Enabled = false; ShowMessageToFront("轉檔啟動"); //壓縮CGI檔案 //DoGGIDataToZip(); //上傳到Google Drive //DoGGIDataToGoogleDrive(); //先做資料夾分類 List <string> lstFile = new List <string>(); ShowMessageToFront("[]FTP-上傳CGI檔案==啟動"); try { // 取得資料夾內所有檔案 int iIndex = 1; string[] FileLoops = Directory.GetFiles(@"D:\M11\Data\20-BackupData\2021", "*", SearchOption.AllDirectories); foreach (string fname in FileLoops) { try { lstFile.Add(fname); FileInfo fi = new FileInfo(fname); string[] CgiNameSplit = fi.Name.Replace(fi.Extension, "").Split('-'); //避免舊檔案格式問題,排除沒有分析完整的檔案名稱 if (CgiNameSplit.Length != 8) { //刪除已處理資料 fi.Delete(); continue; } M11Helper.M11BackupCopyToCGIData(fi); //刪除已處理資料 fi.Delete(); ShowMessageToFront(string.Format("[{0}/{1}]移動CGI檔案 成功=={2}", iIndex.ToString(), FileLoops.Length, fname)); iIndex++; } catch (Exception ex) { //有錯誤持續執行 continue; } } } finally { //client.Disconnect(); //client.Dispose(); } //上傳FTP(10分鐘) //ProcUploadFTP(); //處理本機端檔案壓縮 ShowMessageToFront("轉檔完畢"); } catch (Exception ex) { logger.Error(ex, "M10Winform轉檔錯誤:"); } finally { System.Threading.Thread.Sleep(2000); this.Close(); } }
/// <summary> /// CGI資料上傳到FTP /// </summary> private void UploadFTPCgi() { List <string> lstFile = new List <string>(); ShowMessageToFront("[]FTP-上傳CGI檔案==啟動"); //上傳CgiData FtpClient client = new FtpClient(); try { client.Host = M11Const.FTP_IP; client.SocketKeepAlive = true; client.Credentials = new NetworkCredential(M11Const.FTP_User, M11Const.FTP_Password); client.Connect(); // 取得資料夾內所有檔案 int iIndex = 1; foreach (string fname in Directory.GetFiles(M11Const.Path_FTPQueueTxtOriginal)) { try { lstFile.Add(fname); //FTP上傳路徑規劃 ///M11_System/Data/CgiData/2021/03/21 FileInfo fi = new FileInfo(fname); string[] CgiNameSplit = fi.Name.Replace(fi.Extension, "").Split('-'); //避免舊檔案格式問題,排除沒有分析完整的檔案名稱 if (CgiNameSplit.Length != 8) { continue; } //從檔案取得資料時間 DateTime dt = DateTime.ParseExact(CgiNameSplit[2] + CgiNameSplit[3] + CgiNameSplit[4] + CgiNameSplit[5] + CgiNameSplit[6] + CgiNameSplit[7], "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture); //FluentFTP 起始路徑都是跟目錄開始,目錄結尾都是/ string sLocalPath = fi.FullName; string sRemotePath = "/M11_System/Data/CgiData/"; sRemotePath = string.Format(@"{0}{1}/{2}/{3}/{4}" , sRemotePath, dt.ToString("yyyy"), dt.ToString("MM"), dt.ToString("dd"), fi.Name); //設定嘗試次數 client.RetryAttempts = 3; //上傳檔案 client.UploadFile(sLocalPath, sRemotePath, FtpRemoteExists.Overwrite, true, FtpVerify.Retry); ShowMessageToFront(string.Format("[{0}/{1}]上傳CGI檔案 成功=={2}", iIndex.ToString(), Directory.GetFiles(M11Const.Path_FTPQueueTxtOriginal).Length, fname)); iIndex++; //存至備份資料夾 //fi.CopyTo(Path.Combine(M11Const.Path_FTPQueueTxtOriginalBak, fi.Name), true); // 20210521 直接移動到備份資料夾進行處理 M11Helper.M11BackupCopyToCGIData(fi); //刪除已處理資料 fi.Delete(); } catch (Exception ex) { //有錯誤持續執行 continue; } } } finally { client.Disconnect(); client.Dispose(); } //全部處理完畢再一次刪除 //foreach (string fname in lstFile) //{ // FileInfo fi = new FileInfo(fname); // //存至備份資料夾 // fi.CopyTo(Path.Combine(M11Const.Path_FTPQueueTxtOriginalBak, fi.Name), true); // //刪除已處理資料 // fi.Delete(); //} }