/// <summary> /// 如果線程都結束就讓BUTTON生效 /// </summary> private void EnableButton() { if (this.butOntime.InvokeRequired) { RefreshButton myUI = new RefreshButton(EnableButton); this.butOntime.Invoke(myUI); } else { ThreadCount--; //都結束了 if (ThreadCount <= 0) { #region 備份后刪除操作過的文件 CMDCommad Cmd = new CMDCommad(); for (int j = 0; j < FileList.Items.Count; j++) { try { if (File.Exists(FileList.Items[j].ToString())) { #region 備份操作過的文件 try { FileInfo fi = new FileInfo(FileList.Items[j].ToString()); string sfile = fi.Name; string sPath = "FileCenterBackup\\" + FileList.Items[j].ToString().Replace("\\" + sfile, ""); if (Cmd.CreateDir(sPath)) { File.Copy(FileList.Items[j].ToString(), sPath + "\\" + sfile, true); //xml.WriteXmlRow(LogFile, "Row" + RowNum.ToString().Trim() + string.Format("{0:HHmmssms}", DateTime.Now), "備份文件" + FileList.Items[j].ToString() + "成功!", "Y"); } else { //xml.WriteXmlRow(LogFile, "Row" + RowNum.ToString().Trim() + string.Format("{0:HHmmssms}", DateTime.Now), "備份文件" + FileList.Items[j].ToString() + "失敗!", "Y"); } //RowNum = RowNum + 1; } catch { } #endregion File.Delete(FileList.Items[j].ToString()); } } catch { //xml.WriteXmlRow(LogFile, "Row" + RowNum.ToString().Trim() + string.Format("{0:HHmmssms}", DateTime.Now), "刪除文件" + FileList.Items[j].ToString() + "失敗!", "N"); } } #endregion #region 刷新列表 FileList.Items.Clear(); GetFile(FilePath); #endregion this.butOntime.Enabled = true; this.butQuit.Enabled = true; this.butRefersh.Enabled = true; } } }
/// <summary> /// 子線程更新文件 /// </summary> /// <param name="GroupNo">進程號</param> private void RunSendFileByThread(object tbGroup) { DataTable dtList = (DataTable)tbGroup; RefreshUI UI = new RefreshUI(ShowProgress); //創建委託實例 RefreshView UI_View = new RefreshView(UpdateDone); //創建委託實例 RefreshButton UI_Button = new RefreshButton(EnableButton); //開始遠程拷貝程式 //多臺ap CMDCommad Cmd = new CMDCommad(); XMLReadWrite xml = new XMLReadWrite(); int RowNum = 0; try { foreach (DataRow _dr in dtList.Rows) { string RemoteHost = _dr["ap_name"].ToString(); string UserName = _dr["user_name"].ToString(); string PassWord = _dr["pwd"].ToString(); string RootPath = _dr["root"].ToString(); string AllPath = "\\\\" + RemoteHost + "\\" + _dr["ap_dir"].ToString().Trim().Replace(":", "$") + "\\" + RootPath; string curPath = ""; if (FileList.Items.Count != 0) { #region 傳送文件 this.BeginInvoke(UI_View, new object[] { _dr["ap_seq"].ToString(), "0" }); if (Cmd.Connect(RemoteHost, UserName, PassWord)) { this.BeginInvoke(UI_View, new object[] { _dr["ap_seq"].ToString(), "1" }); for (int j = 0; j < FileList.Items.Count; j++) { FileInfo fi = new FileInfo(FileList.Items[j].ToString()); curPath = AllPath; // 取出子目錄 if (fi.DirectoryName.Length > fi.DirectoryName.LastIndexOf(FilePath) + FilePath.Length) curPath = AllPath + "\\" + fi.DirectoryName.Substring(fi.DirectoryName.LastIndexOf(FilePath) + FilePath.Length + 1, fi.DirectoryName.Length - (fi.DirectoryName.LastIndexOf(FilePath) + FilePath.Length + 1)); //創建目錄 if (Cmd.CreateDir(curPath)) { //xml.WriteXmlRow(LogFile, "Row" + RowNum.ToString().Trim() + string.Format("{0:HHmmssms}", DateTime.Now), "檢查或創建" + AllPath + "目錄成功!", "Y"); } else { //xml.WriteXmlRow(LogFile, "Row" + RowNum.ToString().Trim() + string.Format("{0:HHmmssms}", DateTime.Now), "檢查或創建" + AllPath + "目錄失敗!", "N"); continue; } RowNum = RowNum + 1; //開始拷貝文件 try { File.Copy(FileList.Items[j].ToString(), curPath + "\\" + fi.Name, true); //xml.WriteXmlRow(LogFile, "Row" + RowNum.ToString().Trim() + string.Format("{0:HHmmssms}", DateTime.Now), "Copy File " + FileList.Items[j].ToString() + " -- " + AllPath + " 成功!", "Y"); } catch(Exception ex) { WriteLog("RunSendFileByThread", "複製文件:" + FileList.Items[j].ToString() + "到" + curPath + "\\" + fi.Name + "時出錯:" + (ex != null ? ex.Message : "")); //xml.WriteXmlRow(LogFile, "Row" + RowNum.ToString().Trim() + string.Format("{0:HHmmssms}", DateTime.Now), "Copy File " + FileList.Items[j].ToString() + " -- " + AllPath + " 失敗!", "N"); continue; } RowNum = RowNum + 1; this.BeginInvoke(UI);//UI線程上異步調用委託 //this.BeginInvoke(new ThreadStart(UI)); } } else { //xml.WriteXmlRow(LogFile, "Row" + RowNum.ToString().Trim() + string.Format("{0:HHmmssms}", DateTime.Now), "聯接" + RemoteHost + " AP失敗!", "N"); RowNum = RowNum + 1; continue; } #endregion } if (ConfigFiles.Count != 0) { #region "生成配置文件" //ToDoManager.Launcher.exe.config -> [WCF-IP] [LX-IP] [UP-MODE] [AT-MODE] [AD-MODE] //Update.xml -> [UP-IP] //Manifest.ini -> [ROOTDIR] [VERKEY] // 產生當前服務器文件夾 string cfPath = ConfigPath + "\\" + _dr["ap_seq"].ToString(); if (!Directory.Exists(cfPath)) { Directory.CreateDirectory(cfPath); } string cfile = ""; #region " 1.處理ToDoManager.Launcher.exe.config " cfile = ConfigFiles.Find(f => f.IndexOf("ToDoManager.Launcher.exe.config") > -1); FileInfo fi = new FileInfo(cfile); //複製到服務器文件夾 File.Copy(fi.FullName, cfPath + "\\tmp.config", true); //替換參數 string FileContext = ""; using (StreamReader reader = new StreamReader(cfPath + "\\tmp.config", Encoding.UTF8)) { //[WCF-IP] [LX-IP] [UP-MODE] [AT-MODE] [AD-MODE] FileContext = reader.ReadToEnd(); FileContext = FileContext.Replace("[WCF-IP]", _dr["wcf_ip"].ToString()); FileContext = FileContext.Replace("[LX-IP]", _dr["lx_ip"].ToString()); FileContext = FileContext.Replace("[UP-MODE]", _dr["update_mode"].ToString()); FileContext = FileContext.Replace("[AT-MODE]", _dr["attach_mode"].ToString()); FileContext = FileContext.Replace("[AD-MODE]", _dr["addin_mode"].ToString()); } using (StreamWriter writer = new StreamWriter(cfPath + "\\" + fi.Name, false, Encoding.UTF8)) { writer.Write(FileContext); writer.Flush(); } //複製到目標 //開始拷貝文件 try { File.Copy(fi.DirectoryName + "\\" + _dr["ap_seq"].ToString() + "\\" + fi.Name, AllPath + "\\" + fi.Name, true); //xml.WriteXmlRow(LogFile, "Row" + RowNum.ToString().Trim() + string.Format("{0:HHmmssms}", DateTime.Now), "Copy File " + FileList.Items[j].ToString() + " -- " + AllPath + " 成功!", "Y"); } catch(Exception ex) { WriteLog("RunSendFileByThread", "複製文件:" + fi.DirectoryName + "\\" + _dr["ap_seq"].ToString() + "\\" + fi.Name + "到" + AllPath + "\\" + fi.Name + "時出錯:" + (ex != null ? ex.Message : "")); } #endregion #region " 2.處理Update.xml " cfile = ConfigFiles.Find(f => f.IndexOf("Update.xml") > -1); fi = new FileInfo(cfile); //複製到服務器文件夾 File.Copy(fi.FullName, cfPath + "\\tmp.xml", true); //替換參數 FileContext = ""; using (StreamReader reader = new StreamReader(cfPath + "\\tmp.xml", Encoding.UTF8)) { //[UP-IP] FileContext = reader.ReadToEnd(); FileContext = FileContext.Replace("[UP-IP]", _dr["upsrv_ip"].ToString()); } using (StreamWriter writer = new StreamWriter(cfPath + "\\" + fi.Name, false, Encoding.UTF8)) { writer.Write(FileContext); writer.Flush(); } //複製到目標 //開始拷貝文件 try { File.Copy(fi.DirectoryName + "\\" + _dr["ap_seq"].ToString() + "\\" + fi.Name, AllPath + "\\" + fi.Name, true); //xml.WriteXmlRow(LogFile, "Row" + RowNum.ToString().Trim() + string.Format("{0:HHmmssms}", DateTime.Now), "Copy File " + FileList.Items[j].ToString() + " -- " + AllPath + " 成功!", "Y"); } catch (Exception ex) { WriteLog("RunSendFileByThread", "複製文件:" + fi.DirectoryName + "\\" + _dr["ap_seq"].ToString() + "\\" + fi.Name + "到" + AllPath + "\\" + fi.Name + "時出錯:" + (ex != null ? ex.Message : "")); } #endregion #region " 3.處理Manifest.ini " cfile = ConfigFiles.Find(f => f.IndexOf("Manifest.ini") > -1); fi = new FileInfo(cfile); //複製到服務器文件夾 string m_path = System.IO.Path.Combine(cfPath, fi.Name); File.Copy(fi.FullName, m_path, true); NativeMethods.WritePrivateProfileString("UpdateRootPath", "VALUE", _dr["root"].ToString() + "/", m_path); NativeMethods.WritePrivateProfileString("GUID", "VALUE", VerKey, m_path); ////替換參數 //FileContext = ""; //using (StreamReader reader = new StreamReader(cfPath + "\\tmp.ini", Encoding.UTF8)) //{ // //[ROOTDIR] [VERKEY] // FileContext = reader.ReadToEnd(); // FileContext = FileContext.Replace("[ROOTDIR]", _dr["root"].ToString()); // FileContext = FileContext.Replace("[VERKEY]", VerKey); //} //using (StreamWriter writer = new StreamWriter(cfPath + "\\" + fi.Name, false, Encoding.UTF8)) //{ // writer.Write(FileContext); // writer.Flush(); //} //複製到目標 //開始拷貝文件 try { File.Copy(fi.DirectoryName + "\\" + _dr["ap_seq"].ToString() + "\\" + fi.Name, AllPath + "\\" + fi.Name, true); //xml.WriteXmlRow(LogFile, "Row" + RowNum.ToString().Trim() + string.Format("{0:HHmmssms}", DateTime.Now), "Copy File " + FileList.Items[j].ToString() + " -- " + AllPath + " 成功!", "Y"); } catch (Exception ex) { WriteLog("RunSendFileByThread", "複製文件:" + fi.DirectoryName + "\\" + _dr["ap_seq"].ToString() + "\\" + fi.Name + "到" + AllPath + "\\" + fi.Name + "時出錯:" + (ex != null ? ex.Message : "")); } #endregion #endregion } this.BeginInvoke(UI_View, new object[] { _dr["ap_seq"].ToString(), "2" }); } } catch (Exception ex) { WriteLog("RunSendFileByThread", "出錯了:" + (ex != null ? ex.Message : "")); } finally { // 通知主線程,當前子線程結束 this.BeginInvoke(UI_Button); UI = null; UI_View = null; UI_Button = null; } }