frmMaskUpgrade.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Net;
  10. using System.IO;
  11. using System.Threading;
  12. using System.Drawing.Imaging;
  13. using System.Security.Cryptography.X509Certificates;
  14. using System.Net.Security;
  15. using System.Reflection;
  16. using System.Configuration;
  17. using System.Security.Cryptography;
  18. using System.Threading.Tasks;
  19. using System.Diagnostics;
  20. using HXX.Scanner.Common;
  21. using HXX.Scanner.Biz.Socket;
  22. using HXX.Scanner.Client.Properties;
  23. using HXX.Scanner.Biz;
  24. using Newtonsoft.Json;
  25. namespace HXX.Scanner.Client.UI.mask
  26. {
  27. public partial class frmMaskUpgrade : Form
  28. {
  29. public frmMaskUpgrade(Form main_form, response_http_updateInfo version)
  30. {
  31. InitializeComponent();
  32. this.main_form = main_form;
  33. this.version = version;
  34. }
  35. private Form main_form;
  36. private response_http_updateInfo version;
  37. private void frmMask_Load(object sender, EventArgs e)
  38. {
  39. this.version.data.content = this.version.data.content.Replace("\\r\\n", "\r\n");
  40. this.StartPosition = FormStartPosition.Manual;
  41. this.Location = main_form.Location;
  42. this.Size = main_form.Size;
  43. this.TransparencyKey = Color.Gray;
  44. this.txtContent.Text = this.version.data.content;
  45. this.lblProgress.Select();
  46. download(version);
  47. }
  48. private void download(response_http_updateInfo version)
  49. {
  50. Task.Run(() =>
  51. {
  52. Thread.Sleep(500);
  53. try
  54. {
  55. var url = version.data.url;
  56. var path = System.Windows.Forms.Application.StartupPath + @"\updateTemp";
  57. var fileName = path + @"\updateFile";
  58. var starter = path + @"\HXX.Scanner.UpdateStarter.Console.exe";
  59. DeletePath(path);
  60. download_biz(url, fileName);
  61. if (File.Exists(fileName))
  62. {
  63. showText("下载完成,开始更新...");
  64. Thread.Sleep(2000);
  65. ZipManager.UnZip(fileName, path, "");
  66. //ZipManager.UnZip(@"d:\2\2.zip", path, "");
  67. if (File.Exists(starter))
  68. {
  69. biz_version.write_updateInfo(this.version);
  70. Thread.Sleep(1000);
  71. biz_version.start_starter(starter);
  72. Environment.Exit(0);
  73. }
  74. else
  75. {
  76. showText("未找到更新文件-2");
  77. Thread.Sleep(2000);
  78. }
  79. }
  80. else
  81. {
  82. showText("未找到更新文件-1");
  83. Thread.Sleep(2000);
  84. }
  85. }
  86. catch (Exception ee)
  87. {
  88. LogManager.WriteLog(ee);
  89. showText("发生错误");
  90. MsgManager.Error(ee.Message);
  91. Thread.Sleep(2000);
  92. }
  93. this.Close();
  94. });
  95. }
  96. private void download_biz(string url, string fileName)
  97. {
  98. try
  99. {
  100. HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
  101. req.Method = "GET";
  102. req.ContentLength = 0;
  103. req.ContentType = "application/json";
  104. HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse();
  105. this.Invoke(new Action(() =>
  106. {
  107. this.progressBar1.Minimum = 0;
  108. this.progressBar1.Maximum = Convert.ToInt32(myResponse.ContentLength);
  109. }));
  110. Stream resSt = myResponse.GetResponseStream();
  111. byte[] buffer = new byte[655350];
  112. int count = 0;
  113. using (FileStream fs = File.Create(fileName))
  114. {
  115. count = resSt.Read(buffer, 0, buffer.Length);
  116. while (count > 0)
  117. {
  118. fs.Write(buffer, 0, count);
  119. this.Invoke(new Action(() =>
  120. {
  121. int value = this.progressBar1.Value + count;
  122. if (value > this.progressBar1.Maximum)
  123. {
  124. value = this.progressBar1.Maximum;
  125. }
  126. this.progressBar1.Value = value;
  127. showText(value);
  128. }));
  129. count = resSt.Read(buffer, 0, buffer.Length);
  130. }
  131. }
  132. }
  133. catch (Exception ee)
  134. {
  135. string msg = ee.Message + Environment.NewLine + Environment.NewLine;
  136. msg += "URL:" + url + Environment.NewLine + "Method:GET" + Environment.NewLine;
  137. System.Net.WebException webEE = ee as System.Net.WebException;
  138. if (webEE != null)
  139. {
  140. if (webEE.Response != null)
  141. {
  142. using (System.IO.Stream st = webEE.Response.GetResponseStream())
  143. {
  144. using (System.IO.StreamReader sr = new System.IO.StreamReader(st))
  145. {
  146. string webmsg = sr.ReadToEnd();
  147. msg += Environment.NewLine + Environment.NewLine + webmsg;
  148. }
  149. }
  150. }
  151. }
  152. throw new Exception(msg);
  153. }
  154. }
  155. private void DeletePath(string path)
  156. {
  157. if (Directory.Exists(path))
  158. {
  159. Directory.Delete(path, true);
  160. }
  161. Directory.CreateDirectory(path);
  162. Thread.Sleep(1000);
  163. }
  164. private void showText(int value)
  165. {
  166. var txt = (value * 1.0 / this.progressBar1.Maximum * 100).ToString("0.0");
  167. this.lblProgress.Text = "版本更新 " + txt + "%";
  168. }
  169. private void showText(string msg)
  170. {
  171. this.Invoke(new Action(() =>
  172. {
  173. this.lblProgress.Text = msg;
  174. }));
  175. }
  176. }
  177. }