Form1.cs 11 KB

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