UpgradeOperator.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Threading;
  6. namespace Vinno.FIS.Sonopost.Upgrade
  7. {
  8. public class UpgradeOperator
  9. {
  10. private string _upgradeFilePath;
  11. private string _targetPath;
  12. private bool _deleteAllFiles;
  13. private string _sonopostFolderPath;
  14. public UpgradeOperator(string upgradePath, string targetPath, bool deleteAllFiles)
  15. {
  16. _upgradeFilePath = upgradePath;
  17. _targetPath = targetPath;
  18. _deleteAllFiles = deleteAllFiles;
  19. var rootPath = Path.GetPathRoot(targetPath);
  20. _sonopostFolderPath = Path.Combine(rootPath, "Sonopost");
  21. }
  22. public bool StartUpgrade()
  23. {
  24. if (!Directory.Exists(_upgradeFilePath))
  25. {
  26. return false;
  27. }
  28. UninstallSonopostTask();
  29. StopSonopostKeeper();
  30. StopSonopost();
  31. StopTRTCClient();
  32. RunUpgrade();
  33. InstallSonopostTask();
  34. return true;
  35. }
  36. private void DeleteAllFiles()
  37. {
  38. if (!_deleteAllFiles)
  39. {
  40. return;
  41. }
  42. DeleteDirectory(_sonopostFolderPath);
  43. }
  44. private void RestartSonopostService()
  45. {
  46. ServiceHelper.Install();
  47. ServiceHelper.Start();
  48. }
  49. public void InstallSonopostTask()
  50. {
  51. TaskSchedulerHelper.Install();
  52. }
  53. private void RunUpgrade()
  54. {
  55. MoveFiles(_upgradeFilePath, _targetPath);
  56. DeleteEmptyFolder(_targetPath);
  57. DeleteDirectory(_upgradeFilePath);
  58. DeleteAllFiles();
  59. }
  60. private void StopSonopostService()
  61. {
  62. ServiceHelper.Stop();
  63. ServiceHelper.UnInstall();
  64. }
  65. private void UninstallSonopostTask()
  66. {
  67. TaskSchedulerHelper.UnInstall();
  68. }
  69. private void StopSonopost()
  70. {
  71. FinishProcessByName("Sonopost");
  72. }
  73. private void StopTRTCClient()
  74. {
  75. FinishProcessByName("Vinno.FIS.TRTCClient");
  76. }
  77. private void StopSonopostKeeper()
  78. {
  79. FinishProcessByName("Vinno.FIS.Sonopost.Keeper");
  80. }
  81. private void MoveFiles(string srcFolder, string targetFolder)
  82. {
  83. if (!Directory.Exists(targetFolder))
  84. {
  85. Directory.CreateDirectory(targetFolder);
  86. }
  87. var files = Directory.GetFiles(srcFolder);
  88. foreach (var file in files)
  89. {
  90. try
  91. {
  92. File.Copy(file, file.Replace(srcFolder, targetFolder), true);
  93. }
  94. catch (Exception ex)
  95. {
  96. File.AppendAllText("UpgradeLog.txt", ex.ToString());
  97. }
  98. }
  99. var dirs = Directory.GetDirectories(srcFolder);
  100. foreach (var dir in dirs)
  101. {
  102. if (!dir.EndsWith("SonopostUpgrade"))
  103. {
  104. MoveFiles(dir, dir.Replace(srcFolder, targetFolder));
  105. }
  106. }
  107. }
  108. /// <summary>
  109. /// Delete directory
  110. /// </summary>
  111. /// <param name="directory"></param>
  112. private static bool DeleteDirectory(string directory)
  113. {
  114. try
  115. {
  116. if (!Directory.Exists(directory))
  117. {
  118. return true;
  119. }
  120. var files = Directory.GetFiles(directory);
  121. var directories = Directory.GetDirectories(directory);
  122. foreach (var file in files)
  123. {
  124. File.SetAttributes(file, FileAttributes.Normal);
  125. File.Delete(file);
  126. }
  127. foreach (var dir in directories)
  128. {
  129. DeleteDirectory(dir);
  130. }
  131. Directory.Delete(directory, false);
  132. }
  133. catch (Exception ex)
  134. {
  135. File.AppendAllText("UpgradeLog.txt", ex.ToString());
  136. return false;
  137. }
  138. return true;
  139. }
  140. private bool DeleteEmptyFolder(string folder)
  141. {
  142. bool allChildFolderIsDelete = true;
  143. var files = Directory.GetFiles(folder);
  144. var childFolders = Directory.GetDirectories(folder);
  145. if (files.Length > 0)
  146. {
  147. if (childFolders.Length > 0)
  148. {
  149. foreach (var childFolder in childFolders)
  150. {
  151. if (DeleteEmptyFolder(childFolder) == false)
  152. {
  153. allChildFolderIsDelete = false;
  154. }
  155. }
  156. }
  157. return false;
  158. }
  159. else if (childFolders.Length > 0)
  160. {
  161. foreach (var childFolder in childFolders)
  162. {
  163. if (DeleteEmptyFolder(childFolder) == false)
  164. {
  165. allChildFolderIsDelete = false;
  166. }
  167. }
  168. }
  169. else
  170. {
  171. DeleteDirectory(folder);
  172. return true;
  173. }
  174. if (allChildFolderIsDelete && Directory.Exists(folder))
  175. {
  176. DeleteDirectory(folder);
  177. return true;
  178. }
  179. return false;
  180. }
  181. private bool FinishProcessByName(string name)
  182. {
  183. int count = 5;
  184. while (count > 0)
  185. {
  186. var existApps = Process.GetProcessesByName(name).Where(x => x.ProcessName == name);
  187. if (!existApps.Any())
  188. {
  189. return true;
  190. }
  191. foreach (var item in existApps)
  192. {
  193. item.Kill();
  194. Thread.Sleep(1000);
  195. }
  196. count--;
  197. }
  198. return false;
  199. }
  200. }
  201. }