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(); } /// /// 文件剪切 /// /// /// /// 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); } /// /// 遍历文件 /// /// /// /// private List FindFile(DirectoryInfo di) { var verList = new List(); 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; } /// /// 关闭进程 /// /// 进程名 public void KillProcess(string processName) { Process[] myproc = Process.GetProcesses(); foreach (Process item in myproc) { if (item.ProcessName == processName) { item.Kill(); } } } /// /// 获取文件MD5 /// /// /// 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 ""; } } } }