123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- using Falit.Run.Models;
- using Falit.Run.Service;
- using Falit.Run.Utils;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Diagnostics;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Windows.Forms;
- namespace Falit.Run
- {
- public partial class FrmUpgrade : Form
- {
- private VersionService _versionService;
- public FrmUpgrade()
- {
- InitializeComponent();
- _versionService = new VersionService("", AppConfig._WebApiURL);
- CheckForIllegalCrossThreadCalls = false;
- InitControl();
- }
- private void InitControl()
- {
- dataViewFile.Columns.Add("文件", "文件");
- dataViewFile.Columns.Add("路径", "路径");
- dataViewFile.Columns.Add("进度", "进度");
- dataViewFile.Columns.Add("MB", "MB");
- dataViewFile.Columns.Add("MD5", "MD5");
- dataViewFile.Columns["MD5"].Visible = false;
- dataViewFile.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
- dataViewFile.AllowUserToResizeRows = false;
- dataViewFile.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
- dataViewFile.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
- }
- private int _SumDownLoadSize;
- private int _CurrentDownLoadSize;
- private bool InitData()
- {
- var result = _versionService.AppCheck(new Models.VerAppCheckCondtion
- {
- ApiURL = AppConfig._WebApiURL,
- Ver = "0.0.1"
- });
- if (result.Status == OperateStatus.Success.ToString())
- {
- lbInfo.Text = "系统开始升级...";
- var data = result.Data;
- var newVerPath = AppDomain.CurrentDomain.BaseDirectory + result.Data.Ver;
- if (!Directory.Exists(newVerPath))
- {
- Directory.CreateDirectory(newVerPath);
- }
- this.Text = this.Text + " " + data.Ver;
- lbPercentage.Text = "0 %";
- dataViewFile.Columns.Clear();
- var fieldList = from x in result.Data.VersionFileList
- select new
- {
- 文件 = x.Name,
- 路径 = x.Path,
- 进度 = "0%",
- MB = string.Format("{0:F}", x.Size) + " MB",
- x.MD5
- };
- dataViewFile.DataSource = fieldList.ToList();
- dataViewFile.Columns["MD5"].Visible = false;
- dataViewFile.Columns["路径"].Visible = false;
- dataViewFile.Columns["进度"].Width = dataViewFile.Width / 5;
- dataViewFile.Columns["文件"].Width = dataViewFile.Width / 5;
- dataViewFile.Columns["文件"].Width = dataViewFile.Width / 5 * 3;
- //当前
- var currentPath = AppDomain.CurrentDomain.BaseDirectory;
- var directionInfo = new DirectoryInfo(currentPath);
- var currentFileList = FindFile(directionInfo).Where(m => !m.Name.ToUpper().Contains("App.Config".ToUpper())).ToList();
- //新路径
- var newFilePath = AppDomain.CurrentDomain.BaseDirectory + data.Ver;
- var verFileList = data.VersionFileList.Where(m => !m.Name.ToUpper().Contains("App.Config".ToUpper())).ToList();
- var fileIndex = 0;
- pbrLoading.Maximum = verFileList.Count;
- foreach (var item in verFileList)
- {
- if (currentFileList.Where(m => m.Md5.ToUpper() == item.MD5.ToUpper()).Count() > 0)
- {
- dataViewFile.Rows[fileIndex].DefaultCellStyle.ForeColor = Color.Red;
- dataViewFile.Rows[fileIndex].Cells["进度"].Value = "100%";
- pbrLoading.Value = pbrLoading.Value + 1;
- dataViewFile.FirstDisplayedScrollingRowIndex = dataViewFile.Rows.Count - 1;
- lbPercentage.Text = string.Format("{0:F}",
- (fileIndex + 1) / (float)verFileList.Count() * 100) + "%";
- dataViewFile.CurrentCell = dataViewFile.Rows[fileIndex + 1].Cells[0];
- Application.DoEvents();
- }
- else
- {
- if (File.Exists(newFilePath + @"\" + item.Name))
- {
- File.Delete(newFilePath + @"\" + item.Name);
- }
- if (WebFileDown.DeownloadFile(AppConfig._WebApiURL + item.Path, newFilePath + @"\" + item.Name,
- fileIndex, pbrLoading, lbPercentage, this.dataViewFile))
- {
- dataViewFile.Rows[fileIndex].DefaultCellStyle.ForeColor = Color.Red;
- dataViewFile.Rows[fileIndex].Cells["进度"].Value = "100%";
- pbrLoading.Value = pbrLoading.Value + 1;
- dataViewFile.FirstDisplayedScrollingRowIndex = dataViewFile.Rows.Count - 1;
- lbPercentage.Text = string.Format("{0:F}",
- (fileIndex + 1) / (float)verFileList.Count() * 100) + "%";
- dataViewFile.CurrentCell = dataViewFile.Rows[fileIndex + 1].Cells[0];
- }
- Application.DoEvents();
- }
- fileIndex++;
- }
- pbrLoading.Value = pbrLoading.Maximum;
- lbInfo.Text = "已经更新成功,等待进入系统......";
- for (int i = 0; i < dataViewFile.Rows.Count; i++)
- {
- dataViewFile.DefaultCellStyle.ForeColor = Color.Red;
- dataViewFile.Rows[i].Cells["进度"].Value = "100%";
- }
- return false;
- }
- else
- {
- this.Hide();
- lbInfo.Text = "已是最新版本,无需升级!";
- KillProcess("NXWMS");
- Process.Start(AppDomain.CurrentDomain.BaseDirectory + "NXWMS.exe", "");
- this.Close();
- return true;
- }
- }
- private void FrmUpgrade_Shown(object sender, EventArgs e)
- {
- InitData();
- }
- /// <summary>
- /// 文件剪切
- /// </summary>
- /// <param name="fileName"></param>
- /// <param name="oldFolder"></param>
- /// <param name="newFolder"></param>
- public void FileMove(string fileName, string oldFolder, string newFolder)
- {
- string dirPath = newFolder;
- string defaultPath = oldFolder;
- FileInfo file = new FileInfo(defaultPath);
- file.MoveTo(dirPath + @"\" + file.Name);
- }
- /// <summary>
- /// 遍历文件
- /// </summary>
- /// <param name="di"></param>
- /// <param name="filePath"></param>
- /// <returns></returns>
- private List<ToFileInfo> FindFile(DirectoryInfo di)
- {
- var verList = new List<ToFileInfo>();
- var fis = di.GetFiles().ToList();
- for (int i = 0; i < fis.Count; i++)
- {
- if (!string.IsNullOrWhiteSpace(GetMD5HashFromFile(fis[i].FullName)))
- {
- verList.Add(new ToFileInfo
- {
- Name = fis[i].Name,
- Path = fis[i].FullName,
- Md5 = GetMD5HashFromFile(fis[i].FullName),
- });
- }
- }
- return verList;
- }
- /// <summary>
- /// 关闭进程
- /// </summary>
- /// <param name="processName">进程名</param>
- public void KillProcess(string processName)
- {
- Process[] myproc = Process.GetProcesses();
- foreach (Process item in myproc)
- {
- if (item.ProcessName == processName)
- {
- item.Kill();
- }
- }
- }
- /// <summary>
- /// 获取文件MD5
- /// </summary>
- /// <param name="fileName"></param>
- /// <returns></returns>
- private string GetMD5HashFromFile(string fileName)
- {
- try
- {
- var file = new FileStream(fileName, FileMode.Open);
- System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
- byte[] retVal = md5.ComputeHash(file);
- file.Close();
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < retVal.Length; i++)
- {
- sb.Append(retVal[i].ToString("x2"));
- }
- return sb.ToString();
- }
- catch (Exception ex)
- {
- //throw new Exception("GetMD5HashFromFile() fail,error:" + ex.Message);
- return "";
- }
- }
- }
- }
|