华恒Mes鼎捷代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

178 lines
5.2 KiB

5 months ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Diagnostics;
  7. using System.Windows.Forms;
  8. using System.Threading;
  9. namespace ICSSoft.AutoUpdate.Edition.SetupPatch
  10. {
  11. public class patchSetUp
  12. {
  13. private string CurrEdition = "";
  14. private string CurrFolderPath = "";
  15. private string PathFolderPath = "";
  16. private string ProgramPath = "";
  17. private string CurrExePath = "";
  18. public patchSetUp(string _CurrEdition, string PathFolder, string ProgramName)
  19. {
  20. if (PathFolder == "")
  21. {
  22. PathFolder = "pathTemp";
  23. }
  24. if (ProgramName == "")
  25. {
  26. ProgramName = "ICSSoft.Frame.Main.Entrance.exe";
  27. }
  28. CurrEdition = _CurrEdition;
  29. CurrFolderPath = Directory.GetCurrentDirectory();
  30. PathFolderPath = CurrFolderPath + "\\" + PathFolder;
  31. ProgramPath = CurrFolderPath + "\\" + ProgramName;
  32. string strFullPath = Application.ExecutablePath;
  33. string strFileName = System.IO.Path.GetFileName(strFullPath);
  34. CurrExePath = Path.GetFullPath(CurrFolderPath + "\\" + strFileName);
  35. }
  36. public void Do()
  37. {
  38. DevExpress.Utils.WaitDialogForm _wait = new DevExpress.Utils.WaitDialogForm("正在安装补丁...请稍等....");
  39. _wait.Show();
  40. try
  41. {
  42. ///安装补丁
  43. pathSetUp();
  44. ///删除补丁
  45. ///写入版本号
  46. ///
  47. WriteEdition();
  48. ///运行主程序
  49. ///
  50. RunMainProgram();
  51. }
  52. catch (Exception ex)
  53. {
  54. ///回滚补丁安装
  55. pathRollBack();
  56. throw ex;
  57. }
  58. }
  59. private void pathSetUp()
  60. {
  61. try
  62. {
  63. CopyFolder(PathFolderPath, CurrFolderPath);
  64. }
  65. catch (Exception ex)
  66. {
  67. throw ex;
  68. }
  69. }
  70. private void CopyFolder(string sourcePath, string destPath)
  71. {
  72. if (Directory.Exists(sourcePath))
  73. {
  74. if (!Directory.Exists(destPath))
  75. {
  76. //目标目录不存在则创建
  77. try
  78. {
  79. Directory.CreateDirectory(destPath);
  80. }
  81. catch (Exception ex)
  82. {
  83. throw new Exception("创建目标目录失败:" + ex.Message);
  84. }
  85. }
  86. //获得源文件下所有文件
  87. List<string> files = new List<string>(Directory.GetFiles(sourcePath));
  88. files.ForEach(c =>
  89. {
  90. string destFile = Path.Combine(new string[] { destPath, Path.GetFileName(c) });
  91. if (Path.GetFullPath(destFile) != CurrExePath)
  92. {
  93. File.Copy(c, destFile, true);//覆盖模式
  94. if (System.IO.File.GetAttributes(destFile).ToString().IndexOf("ReadOnly") != -1)
  95. {
  96. File.SetAttributes(destFile, FileAttributes.Normal);
  97. }
  98. }
  99. });
  100. //获得源文件下所有目录文件
  101. List<string> folders = new List<string>(Directory.GetDirectories(sourcePath));
  102. folders.ForEach(c =>
  103. {
  104. string destDir = Path.Combine(new string[] { destPath, Path.GetFileName(c) });
  105. //采用递归的方法实现
  106. CopyFolder(c, destDir);
  107. });
  108. }
  109. else
  110. {
  111. throw new DirectoryNotFoundException("源目录不存在!");
  112. }
  113. }
  114. private void pathRollBack()
  115. {
  116. }
  117. private void WriteEdition()
  118. {
  119. string FilePath = System.Environment.CurrentDirectory;
  120. if (!Directory.Exists(FilePath + "\\BaseServiceConfig"))
  121. {
  122. Directory.CreateDirectory(FilePath + "\\BaseServiceConfig");
  123. }
  124. if (!File.Exists(FilePath + "\\BaseServiceConfig\\Edition.ini"))
  125. {
  126. using (File.Create(FilePath + "\\BaseServiceConfig\\Edition.ini"))
  127. {
  128. }
  129. }
  130. using (StreamWriter sw = new StreamWriter(FilePath + "\\BaseServiceConfig\\Edition.ini"))
  131. {
  132. sw.WriteLine(CurrEdition);
  133. }
  134. }
  135. private void RunMainProgram()
  136. {
  137. if (!File.Exists(ProgramPath))
  138. {
  139. throw new Exception("主程序查找失败,请手动运行");
  140. }
  141. System.Diagnostics.ProcessStartInfo p = null;
  142. System.Diagnostics.Process proc;
  143. p = new System.Diagnostics.ProcessStartInfo(ProgramPath);
  144. proc = System.Diagnostics.Process.Start(p);//调用外部程序
  145. Application.Exit();
  146. }
  147. }
  148. }