Form1.cs 11 KB

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