Program.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Diagnostics;
  7. using System.IO;
  8. using System.Threading;
  9. using HXX.Scanner.Common;
  10. namespace HXX.Scanner.UpdateStarter
  11. {
  12. class Program
  13. {
  14. /// <summary>
  15. /// 程序启动后,复制升级文件到外部,启动外部文件并关闭自身
  16. /// </summary>
  17. /// <param name="args"></param>
  18. static void Main(string[] args)
  19. {
  20. try
  21. {
  22. if (System.AppDomain.CurrentDomain.BaseDirectory.Contains("updateTemp"))
  23. {
  24. Thread.Sleep(1000);
  25. int count = 0;
  26. bool done = true;
  27. while (find_process())
  28. {
  29. Thread.Sleep(100);
  30. count++;
  31. if (count > 50)
  32. {
  33. LogManager.WriteLog("更新失败,原进程无法结束");
  34. done = false;
  35. break;
  36. }
  37. }
  38. if (done)
  39. {
  40. var from = System.AppDomain.CurrentDomain.BaseDirectory;
  41. if (from.EndsWith(@"\"))
  42. {
  43. from = from.Substring(0, from.Length - 1);
  44. }
  45. var to = Directory.GetParent(from).FullName;
  46. var file = to + @"\" + "HXXScannerClient.exe";
  47. CopyDirectory(from, to);
  48. Process proc = new Process();
  49. proc.StartInfo.FileName = file;
  50. proc.Start();
  51. }
  52. }
  53. }
  54. catch (Exception ee)
  55. {
  56. LogManager.WriteLog(ee);
  57. }
  58. }
  59. /// <summary>
  60. /// 杀掉主程序
  61. /// </summary>
  62. private static void kill_process()
  63. {
  64. var pp = Process.GetProcesses();
  65. var count = 0;
  66. foreach (var p in pp)
  67. {
  68. if (p.ProcessName.Contains("HXXScannerClient"))
  69. {
  70. count++;
  71. p.Kill();
  72. }
  73. }
  74. }
  75. /// <summary>
  76. /// 寻找主程序
  77. /// </summary>
  78. /// <returns></returns>
  79. private static bool find_process()
  80. {
  81. var pp = Process.GetProcesses();
  82. var count = 0;
  83. foreach (var p in pp)
  84. {
  85. if (p.ProcessName.Contains("HXXScannerClient"))
  86. {
  87. count++;
  88. }
  89. }
  90. if (count > 0)
  91. {
  92. return true;
  93. }
  94. else
  95. {
  96. return false;
  97. }
  98. }
  99. /// <summary>
  100. /// 拷贝目录
  101. /// </summary>
  102. /// <param name="srcDir"></param>
  103. /// <param name="tgtDir"></param>
  104. static void CopyDirectory(string srcDir, string tgtDir)
  105. {
  106. DirectoryInfo source = new DirectoryInfo(srcDir);
  107. DirectoryInfo target = new DirectoryInfo(tgtDir);
  108. if (target.FullName.StartsWith(source.FullName, StringComparison.CurrentCultureIgnoreCase))
  109. {
  110. throw new Exception("父目录不能拷贝到子目录!");
  111. }
  112. if (!source.Exists)
  113. {
  114. return;
  115. }
  116. if (!target.Exists)
  117. {
  118. target.Create();
  119. }
  120. FileInfo[] files = source.GetFiles();
  121. for (int i = 0; i < files.Length; i++)
  122. {
  123. if (files[i].FullName.Contains("updateFile"))
  124. {
  125. continue;
  126. }
  127. File.Copy(files[i].FullName, target.FullName + @"\" + files[i].Name, true);
  128. }
  129. DirectoryInfo[] dirs = source.GetDirectories();
  130. for (int j = 0; j < dirs.Length; j++)
  131. {
  132. CopyDirectory(dirs[j].FullName, target.FullName + @"\" + dirs[j].Name);
  133. }
  134. }
  135. }
  136. }