| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Diagnostics;
- using System.Threading;
- using System.IO;
- using System.Security.Cryptography;
- using HXX.Scanner.Common;
- namespace HXX.Scanner.Starter
- {
- public partial class frmCheckUpdate : MainForm
- {
- public frmCheckUpdate()
- {
- InitializeComponent();
- }
- public frmCheckUpdate(entity_sub_upgrade_info info)
- {
- InitializeComponent();
- this.upgrade_info = info;
- }
- private entity_sub_upgrade_info upgrade_info;
- private string tempFolder = "";
- private string tempInstallFile = "";
- private string tempUnZipFolder = "";
- private string tempCopier = "";
- private void frmCheckUpdate_Load(object sender, EventArgs e)
- {
- try
- {
- if (upgrade_info.description == null)
- {
- upgrade_info.description = string.Empty;
- }
- this.lbl_server_version.Text = "发现最新版本v" + upgrade_info.version;
- this.lbl_current_version.Text = "当前版本v" + upgrade_info.current_version;
- this.lblText.Text = upgrade_info.description.Replace("\\r\\n", "\r\n");
- if (this.upgrade_info.force == 1)
- {
- this.btnDownload_Click(null, null);
- }
- }
- catch (Exception ee)
- {
- MessageBox.Show("发生错误");
- LogManager.WriteLog(ee);
- Thread.Sleep(1000);
- Environment.Exit(0);
- }
- }
- private void btnCancel_Click(object sender, EventArgs e)
- {
- Process.Start("HXXScannerClient.exe");
- Environment.Exit(0);
- }
- private async void btnDownload_Click(object sender, EventArgs e)
- {
- try
- {
- this.pl_button.Visible = false;
- this.pl_download.Location = this.pl_button.Location;
- this.pl_download.Visible = true;
- if (checkFolder())
- {
- if (await httpDownload(this.upgrade_info.downloadUrl, tempInstallFile, this.progressBar1))
- {
- //if (check_md5(tempInstallFile))
- {
- unzip();
- Thread.Sleep(200);
- if (File.Exists(tempCopier))
- {
- Process process = new Process();
- ProcessStartInfo startInfo = new ProcessStartInfo();
- startInfo.FileName = tempCopier;
- //startInfo.Arguments = "";
- startInfo.WorkingDirectory = tempUnZipFolder;
- //startInfo.RedirectStandardError = true;
- //startInfo.RedirectStandardInput = true;
- //startInfo.RedirectStandardOutput = true;
- startInfo.CreateNoWindow = true;
- startInfo.UseShellExecute = false;
- process.StartInfo = startInfo;
- process.EnableRaisingEvents = false;
- process.Start();
- }
- else
- {
- var msg = "下载的升级文件已损坏";
- MessageBox.Show(msg);
- LogManager.WriteLog(msg);
- Environment.Exit(0);
- }
- //////Process.Start(tempInstallFile);
- //////Environment.Exit(0);
- }
- //else
- //{
- // var msg = "下载的升级文件已损坏";
- // MessageBox.Show(msg);
- // LogManager.WriteLog(msg);
- // Environment.Exit(0);
- //}
- }
- }
- }
- catch (Exception ee)
- {
- MessageBox.Show("发生错误");
- LogManager.WriteLog(ee);
- Environment.Exit(0);
- }
- }
- public async Task<bool> httpDownload(string URL, string filename, System.Windows.Forms.ProgressBar bar)
- {
- return await Task.Run(() =>
- {
- int percent = 0;
- try
- {
- System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL);
- System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse();
- long totalBytes = myrp.ContentLength;
- System.IO.Stream st = myrp.GetResponseStream();
- System.IO.Stream so = new System.IO.FileStream(filename, System.IO.FileMode.Create);
- long totalDownloadedByte = 0;
- byte[] by = new byte[2048];
- int osize = st.Read(by, 0, (int)by.Length);
- while (osize > 0)
- {
- totalDownloadedByte = osize + totalDownloadedByte;
- //System.Windows.Forms.Application.DoEvents();
- so.Write(by, 0, osize);
- osize = st.Read(by, 0, (int)by.Length);
- percent = (int)((float)totalDownloadedByte / (float)totalBytes * 100);
- this.Invoke(new Action(() =>
- {
- bar.Value = percent;
- this.lbl_progress.Text = "版本更新:" + percent.ToString() + "%";
- //Application.DoEvents(); //必须加注这句代码,否则label1将因为循环执行太快而来不及显示信息
- bar.PerformStep();
- }));
- }
- so.Close();
- st.Close();
- return true;
- }
- catch (System.Exception ee)
- {
- this.Invoke(new Action(() =>
- {
- MessageBox.Show("发生错误");
- LogManager.WriteLog(ee);
- Thread.Sleep(1000);
- Environment.Exit(0);
- }));
- return false;
- }
- });
- }
- private bool checkFolder()
- {
- try
- {
- tempFolder = System.IO.Directory.GetCurrentDirectory() + @"\tempDownloading\";
- //tempFolder = @"c:\HXX\tempDownloading\";
- tempInstallFile = tempFolder + "tempInstallFile.zip";
- tempUnZipFolder = tempFolder + @"\unZipFolder\";
- tempCopier = tempUnZipFolder + "HXX.Scanner.Copier.exe";
- KillAllInstallAction();
- if (Directory.Exists(tempFolder))
- {
- Directory.Delete(tempFolder, true);
- }
- Directory.CreateDirectory(tempFolder);
- return true;
- }
- catch (Exception ee)
- {
- MessageBox.Show("发生错误");
- LogManager.WriteLog(ee);
- return false;
- }
- }
- private void unzip()
- {
- if (!string.IsNullOrEmpty(tempInstallFile))
- {
- if (File.Exists(tempInstallFile))
- {
- ZipManager.UnZip(tempInstallFile, tempUnZipFolder, "");
- }
- }
- }
- private void KillAllInstallAction()
- {
- Process[] processes = Process.GetProcessesByName("tempInstallFile.tmp");
- foreach (var p in processes)
- {
- p.Kill();
- Thread.Sleep(200);
- }
- }
- private bool check_md5(string file)
- {
- if (upgrade_info.md5 == CalculateMD5Hash(file))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- public string CalculateMD5Hash(string filePath)
- {
- using (var md5 = MD5.Create())
- {
- using (var stream = File.OpenRead(filePath))
- {
- var hash = md5.ComputeHash(stream);
- return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
- }
- }
- }
- private void roundButton1_MouseEnter(object sender, EventArgs e)
- {
- this.btnDownload.Size = new Size(this.btnDownload.Width + 2, this.btnDownload.Height + 2);
- this.btnDownload.Location = new Point(this.btnDownload.Location.X - 1, this.btnDownload.Location.Y - 1);
- }
- private void roundButton1_MouseLeave(object sender, EventArgs e)
- {
- this.btnDownload.Size = new Size(this.btnDownload.Width - 2, this.btnDownload.Height - 2);
- this.btnDownload.Location = new Point(this.btnDownload.Location.X + 1, this.btnDownload.Location.Y + 1);
- }
- private void btnCancel_MouseEnter(object sender, EventArgs e)
- {
- this.btnCancel.Size = new Size(this.btnCancel.Width + 2, this.btnCancel.Height + 2);
- this.btnCancel.Location = new Point(this.btnCancel.Location.X - 1, this.btnCancel.Location.Y - 1);
- }
- private void btnCancel_MouseLeave(object sender, EventArgs e)
- {
- this.btnCancel.Size = new Size(this.btnCancel.Width - 2, this.btnCancel.Height - 2);
- this.btnCancel.Location = new Point(this.btnCancel.Location.X + 1, this.btnCancel.Location.Y + 1);
- }
- private void lblText_Paint(object sender, PaintEventArgs e)
- {
- int LineDistance = 8;//行间距
- System.Windows.Forms.Label label = sender as System.Windows.Forms.Label;
- System.Drawing.Font drawFont = label.Font;
- label.AutoSize = false;
- SolidBrush drawBrush = new SolidBrush(label.ForeColor);
- Graphics g = e.Graphics;
- g.Clear(label.BackColor);
- StringFormat drawFormat = new StringFormat();
- string[] arrDrawString = label.Text.Split(new char[] { '\n' });
- int height = 0;
- foreach (string str in arrDrawString)
- {
- //文本的矩形区域大小
- SizeF textSize = g.MeasureString(str, label.Font);
- //计算行数
- int strLineCount = Convert.ToInt32(Math.Ceiling(textSize.Width / label.Width));
- height += Convert.ToInt16((textSize.Height + LineDistance) * strLineCount);
- }
- label.Height = height; //计算调整后的高度
- float netTextPos_Y = 0; // 下一行的位置
- foreach (string drawString in arrDrawString)
- {
- bool drawText = false;
- int strLenght = 1; // 长度
- int startIndex = 0; // 开始位置
- for (int i = 0; i < drawString.Length; i++, strLenght++)
- {
- string subN = drawString.Substring(startIndex, strLenght);
- if (startIndex + strLenght >= drawString.Length)
- {
- drawText = true;
- }
- else
- {
- string subN1 = drawString.Substring(startIndex, strLenght + 1);
- if (g.MeasureString(subN, label.Font).Width <= label.Width && g.MeasureString(subN1, label.Font).Width > label.Width)
- {
- drawText = true;
- }
- }
- if (drawText)
- {
- drawText = false;
- strLenght = 0;
- startIndex = i + 1;
- SizeF textSize = g.MeasureString(subN, label.Font);
- e.Graphics.DrawString(subN, drawFont, drawBrush, 0, netTextPos_Y, drawFormat);
- netTextPos_Y = netTextPos_Y + textSize.Height + LineDistance;
- }
- }
- }
- }
- }
- }
|