Form1.cs 11 KB

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