frmCheckUpdate.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Diagnostics;
  11. using System.Threading;
  12. using System.IO;
  13. using System.Security.Cryptography;
  14. using HXX.Scanner.Common;
  15. namespace HXX.Scanner.Starter
  16. {
  17. public partial class frmCheckUpdate : MainForm
  18. {
  19. public frmCheckUpdate()
  20. {
  21. InitializeComponent();
  22. }
  23. public frmCheckUpdate(entity_sub_upgrade_info info)
  24. {
  25. InitializeComponent();
  26. this.upgrade_info = info;
  27. }
  28. private entity_sub_upgrade_info upgrade_info;
  29. private string tempFolder = "";
  30. private string tempInstallFile = "";
  31. private string tempUnZipFolder = "";
  32. private string tempCopier = "";
  33. private void frmCheckUpdate_Load(object sender, EventArgs e)
  34. {
  35. try
  36. {
  37. if (upgrade_info.description == null)
  38. {
  39. upgrade_info.description = string.Empty;
  40. }
  41. this.lbl_server_version.Text = "发现最新版本v" + upgrade_info.version;
  42. this.lbl_current_version.Text = "当前版本v" + upgrade_info.current_version;
  43. this.lblText.Text = upgrade_info.description.Replace("\\r\\n", "\r\n");
  44. if (this.upgrade_info.force == 1)
  45. {
  46. this.btnDownload_Click(null, null);
  47. }
  48. }
  49. catch (Exception ee)
  50. {
  51. MessageBox.Show("发生错误");
  52. LogManager.WriteLog(ee);
  53. Thread.Sleep(1000);
  54. Environment.Exit(0);
  55. }
  56. }
  57. private void btnCancel_Click(object sender, EventArgs e)
  58. {
  59. Process.Start("HXXScannerClient.exe");
  60. Environment.Exit(0);
  61. }
  62. private async void btnDownload_Click(object sender, EventArgs e)
  63. {
  64. try
  65. {
  66. this.pl_button.Visible = false;
  67. this.pl_download.Location = this.pl_button.Location;
  68. this.pl_download.Visible = true;
  69. if (checkFolder())
  70. {
  71. if (await httpDownload(this.upgrade_info.downloadUrl, tempInstallFile, this.progressBar1))
  72. {
  73. //if (check_md5(tempInstallFile))
  74. {
  75. unzip();
  76. Thread.Sleep(200);
  77. if (File.Exists(tempCopier))
  78. {
  79. Process process = new Process();
  80. ProcessStartInfo startInfo = new ProcessStartInfo();
  81. startInfo.FileName = tempCopier;
  82. //startInfo.Arguments = "";
  83. startInfo.WorkingDirectory = tempUnZipFolder;
  84. //startInfo.RedirectStandardError = true;
  85. //startInfo.RedirectStandardInput = true;
  86. //startInfo.RedirectStandardOutput = true;
  87. startInfo.CreateNoWindow = true;
  88. startInfo.UseShellExecute = false;
  89. process.StartInfo = startInfo;
  90. process.EnableRaisingEvents = false;
  91. process.Start();
  92. }
  93. else
  94. {
  95. var msg = "下载的升级文件已损坏";
  96. MessageBox.Show(msg);
  97. LogManager.WriteLog(msg);
  98. Environment.Exit(0);
  99. }
  100. //////Process.Start(tempInstallFile);
  101. //////Environment.Exit(0);
  102. }
  103. //else
  104. //{
  105. // var msg = "下载的升级文件已损坏";
  106. // MessageBox.Show(msg);
  107. // LogManager.WriteLog(msg);
  108. // Environment.Exit(0);
  109. //}
  110. }
  111. }
  112. }
  113. catch (Exception ee)
  114. {
  115. MessageBox.Show("发生错误");
  116. LogManager.WriteLog(ee);
  117. Environment.Exit(0);
  118. }
  119. }
  120. public async Task<bool> httpDownload(string URL, string filename, System.Windows.Forms.ProgressBar bar)
  121. {
  122. return await Task.Run(() =>
  123. {
  124. int percent = 0;
  125. try
  126. {
  127. System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL);
  128. System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse();
  129. long totalBytes = myrp.ContentLength;
  130. System.IO.Stream st = myrp.GetResponseStream();
  131. System.IO.Stream so = new System.IO.FileStream(filename, System.IO.FileMode.Create);
  132. long totalDownloadedByte = 0;
  133. byte[] by = new byte[2048];
  134. int osize = st.Read(by, 0, (int)by.Length);
  135. while (osize > 0)
  136. {
  137. totalDownloadedByte = osize + totalDownloadedByte;
  138. //System.Windows.Forms.Application.DoEvents();
  139. so.Write(by, 0, osize);
  140. osize = st.Read(by, 0, (int)by.Length);
  141. percent = (int)((float)totalDownloadedByte / (float)totalBytes * 100);
  142. this.Invoke(new Action(() =>
  143. {
  144. bar.Value = percent;
  145. this.lbl_progress.Text = "版本更新:" + percent.ToString() + "%";
  146. //Application.DoEvents(); //必须加注这句代码,否则label1将因为循环执行太快而来不及显示信息
  147. bar.PerformStep();
  148. }));
  149. }
  150. so.Close();
  151. st.Close();
  152. return true;
  153. }
  154. catch (System.Exception ee)
  155. {
  156. this.Invoke(new Action(() =>
  157. {
  158. MessageBox.Show("发生错误");
  159. LogManager.WriteLog(ee);
  160. Thread.Sleep(1000);
  161. Environment.Exit(0);
  162. }));
  163. return false;
  164. }
  165. });
  166. }
  167. private bool checkFolder()
  168. {
  169. try
  170. {
  171. tempFolder = System.IO.Directory.GetCurrentDirectory() + @"\tempDownloading\";
  172. //tempFolder = @"c:\HXX\tempDownloading\";
  173. tempInstallFile = tempFolder + "tempInstallFile.zip";
  174. tempUnZipFolder = tempFolder + @"\unZipFolder\";
  175. tempCopier = tempUnZipFolder + "HXX.Scanner.Copier.exe";
  176. KillAllInstallAction();
  177. if (Directory.Exists(tempFolder))
  178. {
  179. Directory.Delete(tempFolder, true);
  180. }
  181. Directory.CreateDirectory(tempFolder);
  182. return true;
  183. }
  184. catch (Exception ee)
  185. {
  186. MessageBox.Show("发生错误");
  187. LogManager.WriteLog(ee);
  188. return false;
  189. }
  190. }
  191. private void unzip()
  192. {
  193. if (!string.IsNullOrEmpty(tempInstallFile))
  194. {
  195. if (File.Exists(tempInstallFile))
  196. {
  197. ZipManager.UnZip(tempInstallFile, tempUnZipFolder, "");
  198. }
  199. }
  200. }
  201. private void KillAllInstallAction()
  202. {
  203. Process[] processes = Process.GetProcessesByName("tempInstallFile.tmp");
  204. foreach (var p in processes)
  205. {
  206. p.Kill();
  207. Thread.Sleep(200);
  208. }
  209. }
  210. private bool check_md5(string file)
  211. {
  212. if (upgrade_info.md5 == CalculateMD5Hash(file))
  213. {
  214. return true;
  215. }
  216. else
  217. {
  218. return false;
  219. }
  220. }
  221. public string CalculateMD5Hash(string filePath)
  222. {
  223. using (var md5 = MD5.Create())
  224. {
  225. using (var stream = File.OpenRead(filePath))
  226. {
  227. var hash = md5.ComputeHash(stream);
  228. return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
  229. }
  230. }
  231. }
  232. private void roundButton1_MouseEnter(object sender, EventArgs e)
  233. {
  234. this.btnDownload.Size = new Size(this.btnDownload.Width + 2, this.btnDownload.Height + 2);
  235. this.btnDownload.Location = new Point(this.btnDownload.Location.X - 1, this.btnDownload.Location.Y - 1);
  236. }
  237. private void roundButton1_MouseLeave(object sender, EventArgs e)
  238. {
  239. this.btnDownload.Size = new Size(this.btnDownload.Width - 2, this.btnDownload.Height - 2);
  240. this.btnDownload.Location = new Point(this.btnDownload.Location.X + 1, this.btnDownload.Location.Y + 1);
  241. }
  242. private void btnCancel_MouseEnter(object sender, EventArgs e)
  243. {
  244. this.btnCancel.Size = new Size(this.btnCancel.Width + 2, this.btnCancel.Height + 2);
  245. this.btnCancel.Location = new Point(this.btnCancel.Location.X - 1, this.btnCancel.Location.Y - 1);
  246. }
  247. private void btnCancel_MouseLeave(object sender, EventArgs e)
  248. {
  249. this.btnCancel.Size = new Size(this.btnCancel.Width - 2, this.btnCancel.Height - 2);
  250. this.btnCancel.Location = new Point(this.btnCancel.Location.X + 1, this.btnCancel.Location.Y + 1);
  251. }
  252. private void lblText_Paint(object sender, PaintEventArgs e)
  253. {
  254. int LineDistance = 8;//行间距
  255. System.Windows.Forms.Label label = sender as System.Windows.Forms.Label;
  256. System.Drawing.Font drawFont = label.Font;
  257. label.AutoSize = false;
  258. SolidBrush drawBrush = new SolidBrush(label.ForeColor);
  259. Graphics g = e.Graphics;
  260. g.Clear(label.BackColor);
  261. StringFormat drawFormat = new StringFormat();
  262. string[] arrDrawString = label.Text.Split(new char[] { '\n' });
  263. int height = 0;
  264. foreach (string str in arrDrawString)
  265. {
  266. //文本的矩形区域大小
  267. SizeF textSize = g.MeasureString(str, label.Font);
  268. //计算行数
  269. int strLineCount = Convert.ToInt32(Math.Ceiling(textSize.Width / label.Width));
  270. height += Convert.ToInt16((textSize.Height + LineDistance) * strLineCount);
  271. }
  272. label.Height = height; //计算调整后的高度
  273. float netTextPos_Y = 0; // 下一行的位置
  274. foreach (string drawString in arrDrawString)
  275. {
  276. bool drawText = false;
  277. int strLenght = 1; // 长度
  278. int startIndex = 0; // 开始位置
  279. for (int i = 0; i < drawString.Length; i++, strLenght++)
  280. {
  281. string subN = drawString.Substring(startIndex, strLenght);
  282. if (startIndex + strLenght >= drawString.Length)
  283. {
  284. drawText = true;
  285. }
  286. else
  287. {
  288. string subN1 = drawString.Substring(startIndex, strLenght + 1);
  289. if (g.MeasureString(subN, label.Font).Width <= label.Width && g.MeasureString(subN1, label.Font).Width > label.Width)
  290. {
  291. drawText = true;
  292. }
  293. }
  294. if (drawText)
  295. {
  296. drawText = false;
  297. strLenght = 0;
  298. startIndex = i + 1;
  299. SizeF textSize = g.MeasureString(subN, label.Font);
  300. e.Graphics.DrawString(subN, drawFont, drawBrush, 0, netTextPos_Y, drawFormat);
  301. netTextPos_Y = netTextPos_Y + textSize.Height + LineDistance;
  302. }
  303. }
  304. }
  305. }
  306. }
  307. }