using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Diagnostics; using System.Windows.Forms; using System.Threading; namespace ICSSoft.AutoUpdate.Edition.SetupPatch { public class patchSetUp { private string CurrEdition = ""; private string CurrFolderPath = ""; private string PathFolderPath = ""; private string ProgramPath = ""; private string CurrExePath = ""; public patchSetUp(string _CurrEdition, string PathFolder, string ProgramName) { if (PathFolder == "") { PathFolder = "pathTemp"; } if (ProgramName == "") { ProgramName = "ICSSoft.Frame.Main.Entrance.exe"; } CurrEdition = _CurrEdition; CurrFolderPath = Directory.GetCurrentDirectory(); PathFolderPath = CurrFolderPath + "\\" + PathFolder; ProgramPath = CurrFolderPath + "\\" + ProgramName; string strFullPath = Application.ExecutablePath; string strFileName = System.IO.Path.GetFileName(strFullPath); CurrExePath = Path.GetFullPath(CurrFolderPath + "\\" + strFileName); } public void Do() { DevExpress.Utils.WaitDialogForm _wait = new DevExpress.Utils.WaitDialogForm("正在安装补丁...请稍等...."); _wait.Show(); try { ///安装补丁 pathSetUp(); ///删除补丁 ///写入版本号 /// WriteEdition(); ///运行主程序 /// RunMainProgram(); } catch (Exception ex) { ///回滚补丁安装 pathRollBack(); throw ex; } } private void pathSetUp() { try { CopyFolder(PathFolderPath, CurrFolderPath); } catch (Exception ex) { throw ex; } } private void CopyFolder(string sourcePath, string destPath) { if (Directory.Exists(sourcePath)) { if (!Directory.Exists(destPath)) { //目标目录不存在则创建 try { Directory.CreateDirectory(destPath); } catch (Exception ex) { throw new Exception("创建目标目录失败:" + ex.Message); } } //获得源文件下所有文件 List files = new List(Directory.GetFiles(sourcePath)); files.ForEach(c => { string destFile = Path.Combine(new string[] { destPath, Path.GetFileName(c) }); if (Path.GetFullPath(destFile) != CurrExePath) { File.Copy(c, destFile, true);//覆盖模式 if (System.IO.File.GetAttributes(destFile).ToString().IndexOf("ReadOnly") != -1) { File.SetAttributes(destFile, FileAttributes.Normal); } } }); //获得源文件下所有目录文件 List folders = new List(Directory.GetDirectories(sourcePath)); folders.ForEach(c => { string destDir = Path.Combine(new string[] { destPath, Path.GetFileName(c) }); //采用递归的方法实现 CopyFolder(c, destDir); }); } else { throw new DirectoryNotFoundException("源目录不存在!"); } } private void pathRollBack() { } private void WriteEdition() { string FilePath = System.Environment.CurrentDirectory; if (!Directory.Exists(FilePath + "\\BaseServiceConfig")) { Directory.CreateDirectory(FilePath + "\\BaseServiceConfig"); } if (!File.Exists(FilePath + "\\BaseServiceConfig\\Edition.ini")) { using (File.Create(FilePath + "\\BaseServiceConfig\\Edition.ini")) { } } using (StreamWriter sw = new StreamWriter(FilePath + "\\BaseServiceConfig\\Edition.ini")) { sw.WriteLine(CurrEdition); } } private void RunMainProgram() { if (!File.Exists(ProgramPath)) { throw new Exception("主程序查找失败,请手动运行"); } System.Diagnostics.ProcessStartInfo p = null; System.Diagnostics.Process proc; p = new System.Diagnostics.ProcessStartInfo(ProgramPath); proc = System.Diagnostics.Process.Start(p);//调用外部程序 Application.Exit(); } } }