FormMain.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. using System;
  2. using System.Windows.Forms;
  3. using SevenZip;
  4. using System.IO;
  5. using System.Threading;
  6. using System.Windows;
  7. namespace AutomaticUpdate
  8. {
  9. public partial class FormMain : Form
  10. {
  11. public FormMain()
  12. {
  13. InitializeComponent();
  14. this.tbc_Main.TabPages.Remove(this.tp_Compress);
  15. this.Shown += FormMain_Shown;
  16. }
  17. //要结束的进程文件路径
  18. private static string killPath = "LYFZ.Software.PhotoStudio.exe";
  19. /// <summary>
  20. /// 要结束的进程文件路径
  21. /// </summary>
  22. public static string KillPath
  23. {
  24. get { return FormMain.killPath; }
  25. set { FormMain.killPath = value; }
  26. }
  27. static string inputCompressFilePath = "";
  28. /// <summary>
  29. /// 输入的解压文件路径
  30. /// </summary>
  31. public static string InputCompressFilePath
  32. {
  33. get { return FormMain.inputCompressFilePath; }
  34. set { FormMain.inputCompressFilePath = value; }
  35. }
  36. public static string runApplicationPath = System.Windows.Forms.Application.StartupPath + "\\LYFZ.Software.PhotoStudio.exe";
  37. void FormMain_Shown(object sender, EventArgs e)
  38. {
  39. // System.Threading.Thread.Sleep(500);
  40. bool create;//LYFZ.Software.PhotoStudio
  41. using (Mutex mu = new Mutex(true, "LYFZ.Software.PhotoStudio", out create))
  42. {
  43. if (create)
  44. {
  45. KillProcessExists(killPath);
  46. b_Extract_Click(null, null);
  47. }
  48. else
  49. {
  50. MessageBox.Show("客户端程序已经在运行,将关闭后重试!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  51. KillProcessExists(killPath);
  52. b_Extract_Click(null, null);
  53. }
  54. }
  55. }
  56. private void KillProcessExists(string _killPath)
  57. {
  58. // string str = Application.ExecutablePath;
  59. try
  60. {
  61. string processesByName = System.IO.Path.GetFileNameWithoutExtension(_killPath);
  62. System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName(processesByName);
  63. foreach (System.Diagnostics.Process p in processes)
  64. {
  65. if (System.IO.Path.GetFileName(_killPath.ToLower()) == System.IO.Path.GetFileName(p.MainModule.FileName.ToLower()))
  66. {
  67. p.Kill();
  68. p.Close();
  69. }
  70. }
  71. }
  72. catch { }
  73. }
  74. private void FormMain_Load(object sender, EventArgs e)
  75. {
  76. //NewLatestVersion.rar
  77. string NewLatestVersionFilePath = System.Windows.Forms.Application.StartupPath + "\\UpdateTemp\\ClientNewLatestVersion";
  78. if (FormMain.inputCompressFilePath.Trim().Length <= 3)
  79. {
  80. if (System.IO.File.Exists(NewLatestVersionFilePath + ".rar"))
  81. {
  82. NewLatestVersionFilePath = NewLatestVersionFilePath + ".rar";
  83. }
  84. else if (System.IO.File.Exists(NewLatestVersionFilePath + ".zip"))
  85. {
  86. NewLatestVersionFilePath = NewLatestVersionFilePath + ".zip";
  87. }
  88. else if (System.IO.File.Exists(NewLatestVersionFilePath + ".7z"))
  89. {
  90. NewLatestVersionFilePath = NewLatestVersionFilePath + ".7z";
  91. }
  92. else
  93. {
  94. NewLatestVersionFilePath = NewLatestVersionFilePath + ".7z";
  95. }
  96. }
  97. else {
  98. NewLatestVersionFilePath = FormMain.inputCompressFilePath.Trim();
  99. }
  100. if (!System.IO.File.Exists(NewLatestVersionFilePath))
  101. {
  102. MessageBox.Show("没有找到相关更新包文件");
  103. this.Close();
  104. return;
  105. }
  106. cb_Format.Items.AddRange(Enum.GetNames(typeof(OutArchiveFormat)));
  107. cb_Method.Items.AddRange(Enum.GetNames(typeof(CompressionMethod)));
  108. cb_Format.SelectedIndex = (int)OutArchiveFormat.SevenZip;
  109. cb_Method.SelectedIndex = (int)CompressionMethod.Default;
  110. // tb_CompressDirectory.Text = @"C:\TEST\新管理软件201517\LYFZ_Server_V150117";
  111. // tb_CompressOutput.Text = @"C:\新管理软件201517.7z";
  112. tb_ExtractDirectory.Text =System.Windows.Forms.Application.StartupPath+ "\\";
  113. tb_ExtractArchive.Text = NewLatestVersionFilePath;
  114. trb_Level.Maximum = Enum.GetNames(typeof(CompressionLevel)).Length - 1;
  115. trb_Level.Value = (int)CompressionLevel.Normal;
  116. trb_Level_Scroll(this, EventArgs.Empty);
  117. }
  118. /// <summary>
  119. /// 运行外部应用程序
  120. /// </summary>
  121. /// <param name="path"></param>
  122. public static void RunStart(string path)
  123. {
  124. FormMain.RunStart(path, "UpdateSuccess");
  125. }
  126. /// <summary>
  127. /// 运行外部应用程序
  128. /// </summary>
  129. /// <param name="path"></param>
  130. public static void RunStart(string path, string arguments = "")
  131. {
  132. try
  133. {
  134. if (System.IO.File.Exists(System.Windows.Forms.Application.StartupPath + path))
  135. {
  136. System.Diagnostics.Process.Start(System.Windows.Forms.Application.StartupPath + path, arguments);
  137. }
  138. else
  139. {
  140. System.Diagnostics.Process.Start(path, arguments);
  141. }
  142. }
  143. catch
  144. {
  145. }
  146. }
  147. private void b_Compress_Click(object sender, EventArgs e)
  148. {
  149. SevenZipCompressor.SetLibraryPath(@"7z.dll");
  150. SevenZipCompressor cmp = new SevenZipCompressor();
  151. cmp.Compressing += new EventHandler<ProgressEventArgs>(cmp_Compressing);
  152. cmp.FileCompressionStarted += new EventHandler<FileNameEventArgs>(cmp_FileCompressionStarted);
  153. cmp.CompressionFinished += new EventHandler<EventArgs>(cmp_CompressionFinished);
  154. cmp.ArchiveFormat = (OutArchiveFormat)Enum.Parse(typeof(OutArchiveFormat), cb_Format.Text);
  155. cmp.CompressionLevel = (CompressionLevel)trb_Level.Value;
  156. cmp.CompressionMethod = (CompressionMethod)cb_Method.SelectedIndex;
  157. cmp.VolumeSize = chb_Volumes.Checked ? (int)nup_VolumeSize.Value : 0;
  158. string directory = tb_CompressDirectory.Text;
  159. string archFileName = tb_CompressOutput.Text;
  160. bool sfxMode = chb_Sfx.Checked;
  161. if (!sfxMode)
  162. {
  163. cmp.BeginCompressDirectory(directory, archFileName);
  164. }
  165. else
  166. {
  167. // Build SevenZipSharp with SFX
  168. /* SevenZipSfx sfx = new SevenZipSfx();
  169. using (MemoryStream ms = new MemoryStream())
  170. {
  171. cmp.CompressDirectory(directory, ms);
  172. sfx.MakeSfx(ms, archFileName.Substring(0, archFileName.LastIndexOf('.')) + ".exe");
  173. }*/
  174. }
  175. }
  176. void cmp_Compressing(object sender, ProgressEventArgs e)
  177. {
  178. pb_CompressProgress.Increment(e.PercentDelta);
  179. }
  180. void cmp_FileCompressionStarted(object sender, FileNameEventArgs e)
  181. {
  182. l_CompressProgress.Text = String.Format("格式 \"{0}\"", e.FileName);
  183. }
  184. void cmp_CompressionFinished(object sender, EventArgs e)
  185. {
  186. l_CompressProgress.Text = "完成";
  187. pb_CompressWork.Style = ProgressBarStyle.Blocks;
  188. pb_CompressProgress.Value = 0;
  189. }
  190. private void b_Browse_Click(object sender, EventArgs e)
  191. {
  192. if (fbd_Directory.ShowDialog() == DialogResult.OK)
  193. {
  194. tb_CompressDirectory.Text = fbd_Directory.SelectedPath;
  195. }
  196. }
  197. private void trb_Level_Scroll(object sender, EventArgs e)
  198. {
  199. l_CompressionLevel.Text = String.Format("压缩级别: {0}", (CompressionLevel)trb_Level.Value);
  200. }
  201. private void b_BrowseOut_Click(object sender, EventArgs e)
  202. {
  203. if (sfd_Archive.ShowDialog() == DialogResult.OK)
  204. {
  205. tb_CompressOutput.Text = sfd_Archive.FileName;
  206. }
  207. }
  208. void extr_ExtractionFinished(object sender, EventArgs e)
  209. {
  210. pb_ExtractWork.Style = ProgressBarStyle.Blocks;
  211. pb_ExtractProgress.Value = 0;
  212. pb_ExtractWork.Value = 0;
  213. l_ExtractProgress.Text = "更新完成";
  214. (sender as SevenZipExtractor).Dispose();
  215. if (System.IO.File.Exists(runApplicationPath))
  216. {
  217. RunStart(runApplicationPath);
  218. }
  219. this.Close();
  220. }
  221. void extr_FileExists(object sender, FileOverwriteEventArgs e)
  222. {
  223. tb_Messages.Text += String.Format("警告: \"{0}\" 已经存在;覆盖\r\n", e.FileName);
  224. }
  225. void extr_FileExtractionStarted(object sender, FileInfoEventArgs e)
  226. {
  227. l_ExtractProgress.Text = String.Format("提取 \"{0}\"", e.FileInfo.FileName);
  228. l_ExtractProgress.Refresh();
  229. pb_ExtractWork.Increment(1);
  230. pb_ExtractWork.Refresh();
  231. }
  232. void extr_Extracting(object sender, ProgressEventArgs e)
  233. {
  234. pb_ExtractProgress.Increment(e.PercentDelta);
  235. pb_ExtractProgress.Refresh();
  236. }
  237. private void b_Extract_Click(object sender, EventArgs e)
  238. {
  239. SevenZipExtractor.SetLibraryPath(@"7z.dll");
  240. string fileName = tb_ExtractArchive.Text;
  241. string directory = tb_ExtractDirectory.Text;
  242. if (System.IO.File.Exists(fileName))
  243. {
  244. try
  245. {
  246. var extr = new SevenZipExtractor(fileName);
  247. pb_ExtractWork.Maximum = (int)extr.FilesCount;
  248. extr.Extracting += new EventHandler<ProgressEventArgs>(extr_Extracting);
  249. extr.FileExtractionStarted += new EventHandler<FileInfoEventArgs>(extr_FileExtractionStarted);
  250. extr.FileExists += new EventHandler<FileOverwriteEventArgs>(extr_FileExists);
  251. extr.ExtractionFinished += new EventHandler<EventArgs>(extr_ExtractionFinished);
  252. System.Threading.Thread.Sleep(200);
  253. extr.BeginExtractArchive(directory);
  254. }
  255. catch(Exception ex) {
  256. MessageBox.Show("更新失败,请关闭正在运行的程序后重试。\r\n错误原因:"+ex.Message);
  257. }
  258. }
  259. else {
  260. if (System.IO.File.Exists(runApplicationPath))
  261. {
  262. RunStart(runApplicationPath);
  263. }
  264. this.Close();
  265. }
  266. }
  267. private void b_ExtractBrowseDirectory_Click(object sender, EventArgs e)
  268. {
  269. if (fbd_Directory.ShowDialog() == DialogResult.OK)
  270. {
  271. tb_ExtractDirectory.Text = fbd_Directory.SelectedPath;
  272. }
  273. }
  274. private void b_ExtractBrowseArchive_Click(object sender, EventArgs e)
  275. {
  276. if (ofd_Archive.ShowDialog() == DialogResult.OK)
  277. {
  278. tb_ExtractArchive.Text = ofd_Archive.FileName;
  279. using (SevenZipExtractor extr = new SevenZipExtractor(ofd_Archive.FileName))
  280. {
  281. var lines = new string[extr.ArchiveFileNames.Count];
  282. extr.ArchiveFileNames.CopyTo(lines, 0);
  283. tb_Messages.Lines = lines;
  284. }
  285. }
  286. }
  287. }
  288. }