Form1.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using System.Windows.Forms;
  8. using Version.Update.Models;
  9. using Y.Utils.DataUtils.Collections;
  10. using Y.Utils.DataUtils.JsonUtils;
  11. using Y.Utils.IOUtils.FileUtils;
  12. using Y.Utils.IOUtils.PathUtils;
  13. using Y.Utils.NetUtils.FTPUtils;
  14. namespace Version.Update
  15. {
  16. public partial class Form1 : Form
  17. {
  18. const string FILE_SUCC = "√";
  19. const string FILE_FAIL = "×";
  20. const string FILE_JUMP = "-";
  21. const int WAIT_TIME = 50;
  22. string AppDir = AppDomain.CurrentDomain.BaseDirectory;
  23. string folder = Guid.NewGuid().ToString();
  24. string downloadPath = "";
  25. string backupPath = "";
  26. VersionModel version;
  27. string VersionFile = @"D:\FTP\Application\version1.0.txt";
  28. string FtpIp = "192.168.3.56";
  29. string FtpAccount = "Administrator";
  30. string FtpPassword = "yuzhengyang";
  31. public Form1()
  32. {
  33. InitializeComponent();
  34. }
  35. private void Form1_Load(object sender, EventArgs e)
  36. {
  37. downloadPath = AppDir + @"VersionUpdate\Download\" + folder;
  38. backupPath = AppDir + @"VersionUpdate\Backup\" + folder;
  39. Task.Factory.StartNew(() =>
  40. {
  41. //获取配置文件 -> 下载文件
  42. if (GetVersion())
  43. {
  44. //DownloadFile(downloadPath);
  45. //BackupFile(backupPath);
  46. //UpdateFile(downloadPath);
  47. //RollBackFile(backupPath);
  48. //Directory.Delete(downloadPath, true);
  49. //Directory.Delete(backupPath, true);
  50. }
  51. });
  52. }
  53. #region 更新功能
  54. /// <summary>
  55. /// 获取版本配置文件
  56. /// </summary>
  57. /// <returns></returns>
  58. bool GetVersion()
  59. {
  60. version = JsonTool.ToObjFromFile<VersionModel>(VersionFile);
  61. if (version != null)
  62. {
  63. try
  64. {
  65. int num = 1;
  66. foreach (var item in version.FileList)
  67. {
  68. this.BeginInvoke(new Action(() => { UIDgvFileListAdd(new object[] { num++, Path.GetFileName(item.File) }); }));
  69. Thread.Sleep(WAIT_TIME);
  70. }
  71. return true;
  72. }
  73. catch (Exception e) { }
  74. }
  75. return false;
  76. }
  77. /// <summary>
  78. /// 下载程序文件
  79. /// </summary>
  80. /// <returns></returns>
  81. bool DownloadFile(string downloadPath)
  82. {
  83. if (DirTool.Create(downloadPath))
  84. {
  85. FileCodeTool fcode = new FileCodeTool();
  86. for (int i = 0; i < version.FileList.Count; i++)
  87. {
  88. string fileName = Path.GetFileName(version.FileList[i].File);
  89. string sourceFile = version.Path + version.FileList[i].File;
  90. string destFile = downloadPath + version.FileList[i].File;
  91. string destPath = destFile.Substring(0, destFile.Length - fileName.Length);
  92. if (DirTool.Create(destPath))
  93. {
  94. try
  95. {
  96. if (File.Exists(AppDir + version.FileList[i].File) &&
  97. version.FileList[i].MD5 == fcode.GetMD5(AppDir + version.FileList[i].File))
  98. {
  99. this.BeginInvoke(new Action(() => { UIDgvFileListUpdate(i, "ColDown", FILE_JUMP); }));
  100. }
  101. else
  102. {
  103. //File.Copy(sourceFile, destFile);
  104. FtpHelper ftp = new FtpHelper(FtpIp, FtpAccount, FtpPassword);
  105. ftp.DownloadFile(sourceFile, destPath);
  106. this.BeginInvoke(new Action(() => { UIDgvFileListUpdate(i, "ColDown", FILE_SUCC); }));
  107. }
  108. }
  109. catch (Exception e)
  110. {
  111. this.BeginInvoke(new Action(() => { UIDgvFileListUpdate(i, "ColDown", FILE_FAIL); }));
  112. }
  113. }
  114. else
  115. {
  116. this.BeginInvoke(new Action(() => { UIDgvFileListUpdate(i, "ColDown", FILE_FAIL); }));
  117. }
  118. Thread.Sleep(WAIT_TIME);
  119. }
  120. }
  121. return false;
  122. }
  123. /// <summary>
  124. /// 备份程序文件
  125. /// </summary>
  126. /// <returns></returns>
  127. bool BackupFile(string backupPath, string downloadPath)
  128. {
  129. if (DirTool.Create(backupPath))
  130. {
  131. FileCodeTool fcode = new FileCodeTool();
  132. for (int i = 0; i < version.FileList.Count; i++)
  133. {
  134. string fileName = Path.GetFileName(version.FileList[i].File);
  135. string sourceFile = AppDir + version.FileList[i].File;
  136. string destFile = backupPath + version.FileList[i].File;
  137. string destPath = destFile.Substring(0, destFile.Length - fileName.Length);
  138. string downloadFile = downloadPath + version.FileList[i].File;
  139. if (DirTool.Create(destPath))
  140. {
  141. try
  142. {
  143. if (File.Exists(sourceFile) && File.Exists(downloadFile) && version.FileList[i].MD5 != fcode.GetMD5(AppDir + version.FileList[i].File))
  144. {
  145. File.Copy(sourceFile, destFile);
  146. this.BeginInvoke(new Action(() => { UIDgvFileListUpdate(i, "ColBack", FILE_SUCC); }));
  147. }
  148. else
  149. {
  150. this.BeginInvoke(new Action(() => { UIDgvFileListUpdate(i, "ColBack", FILE_JUMP); }));
  151. }
  152. }
  153. catch (Exception e)
  154. {
  155. this.BeginInvoke(new Action(() => { UIDgvFileListUpdate(i, "ColBack", FILE_FAIL); }));
  156. }
  157. }
  158. else
  159. {
  160. this.BeginInvoke(new Action(() => { UIDgvFileListUpdate(i, "ColBack", FILE_FAIL); }));
  161. }
  162. Thread.Sleep(WAIT_TIME);
  163. }
  164. }
  165. return false;
  166. }
  167. /// <summary>
  168. /// 更新程序文件
  169. /// </summary>
  170. /// <returns></returns>
  171. bool UpdateFile(string downloadPath)
  172. {
  173. for (int i = 0; i < version.FileList.Count; i++)
  174. {
  175. string fileName = Path.GetFileName(version.FileList[i].File);
  176. string sourceFile = downloadPath + version.FileList[i].File;
  177. string destFile = AppDir + version.FileList[i].File;
  178. string destPath = destFile.Substring(0, destFile.Length - fileName.Length);
  179. if (DirTool.Create(destPath))
  180. {
  181. try
  182. {
  183. if (File.Exists(sourceFile))
  184. {
  185. File.Copy(sourceFile, destFile, true);
  186. this.BeginInvoke(new Action(() => { UIDgvFileListUpdate(i, "ColUpdate", FILE_SUCC); }));
  187. }
  188. else
  189. {
  190. this.BeginInvoke(new Action(() => { UIDgvFileListUpdate(i, "ColUpdate", FILE_JUMP); }));
  191. }
  192. }
  193. catch (Exception e)
  194. {
  195. this.BeginInvoke(new Action(() => { UIDgvFileListUpdate(i, "ColUpdate", FILE_FAIL); }));
  196. }
  197. }
  198. else
  199. {
  200. this.BeginInvoke(new Action(() => { UIDgvFileListUpdate(i, "ColBack", FILE_FAIL); }));
  201. }
  202. Thread.Sleep(WAIT_TIME);
  203. }
  204. return false;
  205. }
  206. /// <summary>
  207. /// 还原程序文件
  208. /// </summary>
  209. /// <returns></returns>
  210. bool RollBackFile(string backupPath)
  211. {
  212. for (int i = 0; i < version.FileList.Count; i++)
  213. {
  214. string fileName = Path.GetFileName(version.FileList[i].File);
  215. string sourceFile = backupPath + version.FileList[i].File;
  216. string destFile = AppDir + version.FileList[i].File;
  217. string destPath = destFile.Substring(0, destFile.Length - fileName.Length);
  218. if (DirTool.Create(destPath))
  219. {
  220. try
  221. {
  222. if (File.Exists(sourceFile))
  223. {
  224. File.Copy(sourceFile, destFile, true);
  225. this.BeginInvoke(new Action(() => { UIDgvFileListUpdate(i, "ColRoll", FILE_SUCC); }));
  226. }
  227. else
  228. {
  229. this.BeginInvoke(new Action(() => { UIDgvFileListUpdate(i, "ColRoll", FILE_JUMP); }));
  230. }
  231. }
  232. catch (Exception e)
  233. {
  234. this.BeginInvoke(new Action(() => { UIDgvFileListUpdate(i, "ColRoll", FILE_FAIL); }));
  235. }
  236. }
  237. else
  238. {
  239. this.BeginInvoke(new Action(() => { UIDgvFileListUpdate(i, "ColRoll", FILE_FAIL); }));
  240. }
  241. Thread.Sleep(WAIT_TIME);
  242. }
  243. return false;
  244. }
  245. /// <summary>
  246. /// 清理之前版本遗留文件及空文件夹
  247. /// </summary>
  248. /// <returns></returns>
  249. void CleanFile()
  250. {
  251. #region 删除非当前版本文件
  252. List<string> file = FileTool.GetAllFile(AppDir);
  253. if (!ListTool.IsNullOrEmpty(file))
  254. {
  255. foreach (var f in file)
  256. {
  257. int c = version.FileList.Where(x => x.File == "\\" + f.Replace(AppDir, "")).Count();
  258. if (c == 0)
  259. {
  260. File.Delete(f);
  261. }
  262. }
  263. Thread.Sleep(WAIT_TIME);
  264. }
  265. #endregion
  266. #region 删除空文件夹
  267. List<string> path = DirTool.GetAllPath(AppDir);
  268. if (!ListTool.IsNullOrEmpty(path))
  269. {
  270. path = path.OrderByDescending(x => x).ToList();
  271. foreach (var p in path)
  272. {
  273. if (Directory.GetFiles(p).Length == 0 && Directory.GetDirectories(p).Length == 0)
  274. {
  275. if (Directory.Exists(p))
  276. {
  277. Directory.Delete(p);
  278. }
  279. }
  280. Thread.Sleep(WAIT_TIME);
  281. }
  282. }
  283. #endregion
  284. }
  285. #endregion
  286. #region UI刷新
  287. /// <summary>
  288. /// 在DgvFileList中添加一条新纪录
  289. /// </summary>
  290. /// <param name="values"></param>
  291. void UIDgvFileListAdd(params object[] values)
  292. {
  293. if (values != null)
  294. {
  295. DgvFileList.Rows.Add(values);
  296. }
  297. }
  298. void UIDgvFileListUpdate(int row, string cell, string value)
  299. {
  300. DgvFileList.Rows[row].Cells[cell].Value = value;
  301. PbStatus.Value = (int)((double)(row + 1) / version.FileList.Count * 100);
  302. }
  303. #endregion
  304. private void BtDownload_Click(object sender, EventArgs e)
  305. {
  306. Task.Factory.StartNew(() => { DownloadFile(downloadPath); });
  307. }
  308. private void BtBackup_Click(object sender, EventArgs e)
  309. {
  310. Task.Factory.StartNew(() => { BackupFile(backupPath, downloadPath); });
  311. }
  312. private void BtUpdate_Click(object sender, EventArgs e)
  313. {
  314. Task.Factory.StartNew(() => { UpdateFile(downloadPath); });
  315. }
  316. private void BtRollback_Click(object sender, EventArgs e)
  317. {
  318. Task.Factory.StartNew(() => { RollBackFile(backupPath); });
  319. }
  320. private void BtClean_Click(object sender, EventArgs e)
  321. {
  322. Task.Factory.StartNew(() => { CleanFile(); });
  323. }
  324. }
  325. }