frmProduceUpdate.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using ICSharpCode.SharpZipLib.Zip;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Diagnostics;
  8. using System.Drawing;
  9. using System.Linq;
  10. using System.Net;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. namespace WDProduceToolUpdate
  15. {
  16. public partial class frmProduceUpdate : Form
  17. {
  18. public frmProduceUpdate()
  19. {
  20. InitializeComponent();
  21. }
  22. WdProduceToolVersion version = null;
  23. string updateFileName = string.Empty;
  24. private void update_Load(object sender, EventArgs e)
  25. {
  26. updateprocess();
  27. }
  28. private void updateprocess()
  29. {
  30. try
  31. {
  32. string versionStr = JsonFileHelper.GetJsonFile(AppDomain.CurrentDomain.BaseDirectory + "version.json");
  33. if (string.IsNullOrEmpty(versionStr))
  34. {
  35. string message = string.Format("获取更新文件失败!");
  36. if (DialogResult.OK == MessageBox.Show(message, "系统错误", MessageBoxButtons.OK))
  37. {
  38. Application.Exit();
  39. }
  40. }
  41. if (!Directory.Exists(Application.StartupPath + @"update"))
  42. {
  43. Directory.CreateDirectory(Application.StartupPath + @"update");
  44. }
  45. version = JsonConvert.DeserializeObject<WdProduceToolVersion>(versionStr);
  46. string[] filePaths = version.update_file_path.Split(@"/");
  47. updateFileName= filePaths[filePaths.Length - 1];
  48. WebClient wc = new WebClient();
  49. wc.DownloadProgressChanged += wc_DownloadProgressChanged;
  50. wc.DownloadFileAsync(new Uri(version.server_url + @"/" + version.update_file_path), Application.StartupPath + @"update\"+ updateFileName);
  51. }
  52. catch (Exception er)
  53. {
  54. label1.Text = "下载失败:" + er.Message;
  55. }
  56. }
  57. private void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
  58. {
  59. Action act = () =>
  60. {
  61. this.progressBar1.Value = e.ProgressPercentage;
  62. this.label1.Text = "正在下载...";
  63. //this.label1.Text = e.ProgressPercentage + "%";
  64. };
  65. this.Invoke(act);
  66. if (e.ProgressPercentage == 100)
  67. {
  68. //下载完成之后开始覆盖
  69. this.label1.Text = "正在解压...";
  70. try
  71. {
  72. var result = UnZip(Application.StartupPath + @"update\"+ updateFileName, Application.StartupPath,"");
  73. //var result = unZip(Application.StartupPath + "\\update.rar", @"..\LWH\");
  74. if (result)
  75. {
  76. progressBar1.Value = 100;
  77. this.label1.Text = "准备安装...";
  78. this.label1.Text = "更新完成";
  79. string versionStr = JsonFileHelper.GetJsonFile(AppDomain.CurrentDomain.BaseDirectory + "version.json");
  80. version = JsonConvert.DeserializeObject<WdProduceToolVersion>(versionStr);
  81. version.updated = true;
  82. JsonFileHelper.WriteJsonFile(AppDomain.CurrentDomain.BaseDirectory + "version.json", JsonConvert.SerializeObject(version));
  83. var mainFile = Application.StartupPath + "WDProduceTool.exe";//重新启动软件
  84. Process p = new Process();
  85. p.StartInfo.UseShellExecute = false;
  86. p.StartInfo.RedirectStandardOutput = true;
  87. p.StartInfo.FileName = mainFile;
  88. p.StartInfo.CreateNoWindow = true;
  89. p.Start();
  90. this.Close();
  91. }
  92. else
  93. {
  94. MessageBox.Show("更新失败:" + result);
  95. this.Close();
  96. }
  97. }
  98. catch (Exception ex)
  99. {
  100. MessageBox.Show(ex.Message, "更新失败");
  101. this.Close();
  102. }
  103. }
  104. }
  105. /// <summary>
  106. /// ZIP:解压一个zip文件
  107. /// add yuangang by 2016-06-13
  108. /// </summary>
  109. /// <param name="ZipFile">需要解压的Zip文件(绝对路径)</param>
  110. /// <param name="TargetDirectory">解压到的目录</param>
  111. /// <param name="Password">解压密码</param>
  112. /// <param name="OverWrite">是否覆盖已存在的文件</param>
  113. public bool UnZip(string ZipFile, string TargetDirectory, string Password, bool OverWrite = true)
  114. {
  115. //如果解压到的目录不存在,则报错
  116. if (!System.IO.Directory.Exists(TargetDirectory))
  117. {
  118. throw new System.IO.FileNotFoundException("指定的目录: " + TargetDirectory + " 不存在!");
  119. }
  120. //目录结尾
  121. if (!TargetDirectory.EndsWith("\\")) { TargetDirectory = TargetDirectory + "\\"; }
  122. using (ZipInputStream zipfiles = new ZipInputStream(File.OpenRead(ZipFile)))
  123. {
  124. zipfiles.Password = Password;
  125. ZipEntry theEntry;
  126. while ((theEntry = zipfiles.GetNextEntry()) != null)
  127. {
  128. string directoryName = "";
  129. string pathToZip = "";
  130. pathToZip = theEntry.Name;
  131. if (pathToZip != "")
  132. directoryName = Path.GetDirectoryName(pathToZip) + "\\";
  133. string fileName = Path.GetFileName(pathToZip);
  134. Directory.CreateDirectory(TargetDirectory + directoryName);
  135. if (fileName != "")
  136. {
  137. if ((File.Exists(TargetDirectory + directoryName + fileName) && OverWrite) || (!File.Exists(TargetDirectory + directoryName + fileName)))
  138. {
  139. using (FileStream streamWriter = File.Create(TargetDirectory + directoryName + fileName))
  140. {
  141. int size = 2048;
  142. byte[] data = new byte[2048];
  143. while (true)
  144. {
  145. size = zipfiles.Read(data, 0, data.Length);
  146. if (size > 0)
  147. streamWriter.Write(data, 0, size);
  148. else
  149. break;
  150. }
  151. streamWriter.Close();
  152. }
  153. }
  154. }
  155. }
  156. zipfiles.Close();
  157. return true;
  158. }
  159. }
  160. }
  161. }