private void SaveXML(string FileName, DataGridView GridView) { LRSecurity lrSecurity = new LRSecurity(); try { if (File.Exists(FileName)) { File.Delete(FileName); } if (GridView.Rows.Count == 0) return; StreamWriter fs; fs = new StreamWriter(FileName, true, Encoding.UTF8); fs.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); fs.WriteLine("<root>"); fs.WriteLine("</root>"); fs.Flush(); fs.Close(); XMLReadWrite xml = new XMLReadWrite(); string sRow = "", sCol = "", sValue = ""; for (int i = 0; i < GridView.Rows.Count; i++) { sRow = "Row" + i.ToString(); for (int j = 0; j < GridView.Columns.Count - 1; j++) { sCol = "Columns" + j.ToString(); if (j == 3) { sValue = lrSecurity.DESEncrypt(GridView.Rows[i].Cells[j].Value.ToString()); } else { if (GridView.Rows[i].Cells[j].Value != null) sValue = GridView.Rows[i].Cells[j].Value.ToString(); else sValue = GridView.Rows[i].Cells[j].EditedFormattedValue.ToString(); } xml.WriteXML(FileName, sRow, sCol, sValue); } } } catch (Exception g) { throw g; } }
/// <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; } }