FrmUpgrade.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using Falit.Run.Models;
  2. using Falit.Run.Service;
  3. using Falit.Run.Utils;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Diagnostics;
  9. using System.Drawing;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading;
  14. using System.Windows.Forms;
  15. namespace Falit.Run
  16. {
  17. public partial class FrmUpgrade : Form
  18. {
  19. private VersionService _versionService;
  20. public FrmUpgrade()
  21. {
  22. InitializeComponent();
  23. _versionService = new VersionService("", AppConfig._WebApiURL);
  24. CheckForIllegalCrossThreadCalls = false;
  25. InitControl();
  26. }
  27. private void InitControl()
  28. {
  29. dataViewFile.Columns.Add("文件", "文件");
  30. dataViewFile.Columns.Add("路径", "路径");
  31. dataViewFile.Columns.Add("进度", "进度");
  32. dataViewFile.Columns.Add("MB", "MB");
  33. dataViewFile.Columns.Add("MD5", "MD5");
  34. dataViewFile.Columns["MD5"].Visible = false;
  35. dataViewFile.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
  36. dataViewFile.AllowUserToResizeRows = false;
  37. dataViewFile.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
  38. dataViewFile.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
  39. }
  40. private int _SumDownLoadSize;
  41. private int _CurrentDownLoadSize;
  42. private bool InitData()
  43. {
  44. var result = _versionService.AppCheck(new Models.VerAppCheckCondtion
  45. {
  46. ApiURL = AppConfig._WebApiURL,
  47. Ver = "0.0.1"
  48. });
  49. if (result.Status == OperateStatus.Success.ToString())
  50. {
  51. lbInfo.Text = "系统开始升级...";
  52. var data = result.Data;
  53. var newVerPath = AppDomain.CurrentDomain.BaseDirectory + result.Data.Ver;
  54. if (!Directory.Exists(newVerPath))
  55. {
  56. Directory.CreateDirectory(newVerPath);
  57. }
  58. this.Text = this.Text + " " + data.Ver;
  59. lbPercentage.Text = "0 %";
  60. dataViewFile.Columns.Clear();
  61. var fieldList = from x in result.Data.VersionFileList
  62. select new
  63. {
  64. 文件 = x.Name,
  65. 路径 = x.Path,
  66. 进度 = "0%",
  67. MB = string.Format("{0:F}", x.Size) + " MB",
  68. x.MD5
  69. };
  70. dataViewFile.DataSource = fieldList.ToList();
  71. dataViewFile.Columns["MD5"].Visible = false;
  72. dataViewFile.Columns["路径"].Visible = false;
  73. dataViewFile.Columns["进度"].Width = dataViewFile.Width / 5;
  74. dataViewFile.Columns["文件"].Width = dataViewFile.Width / 5;
  75. dataViewFile.Columns["文件"].Width = dataViewFile.Width / 5 * 3;
  76. //当前
  77. var currentPath = AppDomain.CurrentDomain.BaseDirectory;
  78. var directionInfo = new DirectoryInfo(currentPath);
  79. var currentFileList = FindFile(directionInfo).Where(m => !m.Name.ToUpper().Contains("App.Config".ToUpper())).ToList();
  80. //新路径
  81. var newFilePath = AppDomain.CurrentDomain.BaseDirectory + data.Ver;
  82. var verFileList = data.VersionFileList.Where(m => !m.Name.ToUpper().Contains("App.Config".ToUpper())).ToList();
  83. var fileIndex = 0;
  84. pbrLoading.Maximum = verFileList.Count;
  85. foreach (var item in verFileList)
  86. {
  87. if (currentFileList.Where(m => m.Md5.ToUpper() == item.MD5.ToUpper()).Count() > 0)
  88. {
  89. dataViewFile.Rows[fileIndex].DefaultCellStyle.ForeColor = Color.Red;
  90. dataViewFile.Rows[fileIndex].Cells["进度"].Value = "100%";
  91. pbrLoading.Value = pbrLoading.Value + 1;
  92. dataViewFile.FirstDisplayedScrollingRowIndex = dataViewFile.Rows.Count - 1;
  93. lbPercentage.Text = string.Format("{0:F}",
  94. (fileIndex + 1) / (float)verFileList.Count() * 100) + "%";
  95. dataViewFile.CurrentCell = dataViewFile.Rows[fileIndex + 1].Cells[0];
  96. Application.DoEvents();
  97. }
  98. else
  99. {
  100. if (File.Exists(newFilePath + @"\" + item.Name))
  101. {
  102. File.Delete(newFilePath + @"\" + item.Name);
  103. }
  104. if (WebFileDown.DeownloadFile(AppConfig._WebApiURL + item.Path, newFilePath + @"\" + item.Name,
  105. fileIndex, pbrLoading, lbPercentage, this.dataViewFile))
  106. {
  107. dataViewFile.Rows[fileIndex].DefaultCellStyle.ForeColor = Color.Red;
  108. dataViewFile.Rows[fileIndex].Cells["进度"].Value = "100%";
  109. pbrLoading.Value = pbrLoading.Value + 1;
  110. dataViewFile.FirstDisplayedScrollingRowIndex = dataViewFile.Rows.Count - 1;
  111. lbPercentage.Text = string.Format("{0:F}",
  112. (fileIndex + 1) / (float)verFileList.Count() * 100) + "%";
  113. dataViewFile.CurrentCell = dataViewFile.Rows[fileIndex + 1].Cells[0];
  114. }
  115. Application.DoEvents();
  116. }
  117. fileIndex++;
  118. }
  119. pbrLoading.Value = pbrLoading.Maximum;
  120. lbInfo.Text = "已经更新成功,等待进入系统......";
  121. for (int i = 0; i < dataViewFile.Rows.Count; i++)
  122. {
  123. dataViewFile.DefaultCellStyle.ForeColor = Color.Red;
  124. dataViewFile.Rows[i].Cells["进度"].Value = "100%";
  125. }
  126. return false;
  127. }
  128. else
  129. {
  130. this.Hide();
  131. lbInfo.Text = "已是最新版本,无需升级!";
  132. KillProcess("NXWMS");
  133. Process.Start(AppDomain.CurrentDomain.BaseDirectory + "NXWMS.exe", "");
  134. this.Close();
  135. return true;
  136. }
  137. }
  138. private void FrmUpgrade_Shown(object sender, EventArgs e)
  139. {
  140. InitData();
  141. }
  142. /// <summary>
  143. /// 文件剪切
  144. /// </summary>
  145. /// <param name="fileName"></param>
  146. /// <param name="oldFolder"></param>
  147. /// <param name="newFolder"></param>
  148. public void FileMove(string fileName, string oldFolder, string newFolder)
  149. {
  150. string dirPath = newFolder;
  151. string defaultPath = oldFolder;
  152. FileInfo file = new FileInfo(defaultPath);
  153. file.MoveTo(dirPath + @"\" + file.Name);
  154. }
  155. /// <summary>
  156. /// 遍历文件
  157. /// </summary>
  158. /// <param name="di"></param>
  159. /// <param name="filePath"></param>
  160. /// <returns></returns>
  161. private List<ToFileInfo> FindFile(DirectoryInfo di)
  162. {
  163. var verList = new List<ToFileInfo>();
  164. var fis = di.GetFiles().ToList();
  165. for (int i = 0; i < fis.Count; i++)
  166. {
  167. if (!string.IsNullOrWhiteSpace(GetMD5HashFromFile(fis[i].FullName)))
  168. {
  169. verList.Add(new ToFileInfo
  170. {
  171. Name = fis[i].Name,
  172. Path = fis[i].FullName,
  173. Md5 = GetMD5HashFromFile(fis[i].FullName),
  174. });
  175. }
  176. }
  177. return verList;
  178. }
  179. /// <summary>
  180. /// 关闭进程
  181. /// </summary>
  182. /// <param name="processName">进程名</param>
  183. public void KillProcess(string processName)
  184. {
  185. Process[] myproc = Process.GetProcesses();
  186. foreach (Process item in myproc)
  187. {
  188. if (item.ProcessName == processName)
  189. {
  190. item.Kill();
  191. }
  192. }
  193. }
  194. /// <summary>
  195. /// 获取文件MD5
  196. /// </summary>
  197. /// <param name="fileName"></param>
  198. /// <returns></returns>
  199. private string GetMD5HashFromFile(string fileName)
  200. {
  201. try
  202. {
  203. var file = new FileStream(fileName, FileMode.Open);
  204. System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
  205. byte[] retVal = md5.ComputeHash(file);
  206. file.Close();
  207. StringBuilder sb = new StringBuilder();
  208. for (int i = 0; i < retVal.Length; i++)
  209. {
  210. sb.Append(retVal[i].ToString("x2"));
  211. }
  212. return sb.ToString();
  213. }
  214. catch (Exception ex)
  215. {
  216. //throw new Exception("GetMD5HashFromFile() fail,error:" + ex.Message);
  217. return "";
  218. }
  219. }
  220. }
  221. }