using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net; using System.IO; using System.Threading; using System.Drawing.Imaging; using System.Security.Cryptography.X509Certificates; using System.Net.Security; using System.Reflection; using System.Configuration; using System.Security.Cryptography; using System.Threading.Tasks; using System.Diagnostics; using HXX.Scanner.Common; using HXX.Scanner.Biz.Socket; using HXX.Scanner.Client.Properties; using HXX.Scanner.Biz; using Newtonsoft.Json; namespace HXX.Scanner.Client.UI.mask { public partial class frmMaskUpgrade : Form { public frmMaskUpgrade(Form main_form, response_http_updateInfo version) { InitializeComponent(); this.main_form = main_form; this.version = version; } private Form main_form; private response_http_updateInfo version; private void frmMask_Load(object sender, EventArgs e) { this.version.data.content = this.version.data.content.Replace("\\r\\n", "\r\n"); this.StartPosition = FormStartPosition.Manual; this.Location = main_form.Location; this.Size = main_form.Size; this.TransparencyKey = Color.Gray; this.txtContent.Text = this.version.data.content; this.lblProgress.Select(); download(version); } private void download(response_http_updateInfo version) { Task.Run(() => { Thread.Sleep(500); try { var url = version.data.url; var path = System.Windows.Forms.Application.StartupPath + @"\updateTemp"; var fileName = path + @"\updateFile"; var starter = path + @"\HXX.Scanner.UpdateStarter.Console.exe"; DeletePath(path); download_biz(url, fileName); if (File.Exists(fileName)) { showText("下载完成,开始更新..."); Thread.Sleep(2000); ZipManager.UnZip(fileName, path, ""); //ZipManager.UnZip(@"d:\2\2.zip", path, ""); if (File.Exists(starter)) { biz_version.write_updateInfo(this.version); Thread.Sleep(1000); biz_version.start_starter(starter); Environment.Exit(0); } else { showText("未找到更新文件-2"); Thread.Sleep(2000); } } else { showText("未找到更新文件-1"); Thread.Sleep(2000); } } catch (Exception ee) { LogManager.WriteLog(ee); showText("发生错误"); MsgManager.Error(ee.Message); Thread.Sleep(2000); } this.Close(); }); } private void download_biz(string url, string fileName) { try { HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.Method = "GET"; req.ContentLength = 0; req.ContentType = "application/json"; HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse(); this.Invoke(new Action(() => { this.progressBar1.Minimum = 0; this.progressBar1.Maximum = Convert.ToInt32(myResponse.ContentLength); })); Stream resSt = myResponse.GetResponseStream(); byte[] buffer = new byte[655350]; int count = 0; using (FileStream fs = File.Create(fileName)) { count = resSt.Read(buffer, 0, buffer.Length); while (count > 0) { fs.Write(buffer, 0, count); this.Invoke(new Action(() => { int value = this.progressBar1.Value + count; if (value > this.progressBar1.Maximum) { value = this.progressBar1.Maximum; } this.progressBar1.Value = value; showText(value); })); count = resSt.Read(buffer, 0, buffer.Length); } } } catch (Exception ee) { string msg = ee.Message + Environment.NewLine + Environment.NewLine; msg += "URL:" + url + Environment.NewLine + "Method:GET" + Environment.NewLine; System.Net.WebException webEE = ee as System.Net.WebException; if (webEE != null) { if (webEE.Response != null) { using (System.IO.Stream st = webEE.Response.GetResponseStream()) { using (System.IO.StreamReader sr = new System.IO.StreamReader(st)) { string webmsg = sr.ReadToEnd(); msg += Environment.NewLine + Environment.NewLine + webmsg; } } } } throw new Exception(msg); } } private void DeletePath(string path) { if (Directory.Exists(path)) { Directory.Delete(path, true); } Directory.CreateDirectory(path); Thread.Sleep(1000); } private void showText(int value) { var txt = (value * 1.0 / this.progressBar1.Maximum * 100).ToString("0.0"); this.lblProgress.Text = "版本更新 " + txt + "%"; } private void showText(string msg) { this.Invoke(new Action(() => { this.lblProgress.Text = msg; })); } } }