AppUpdateTool.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //************************************************************************
  2. // https://github.com/yuzhengyang
  3. // author: yuzhengyang
  4. // date: 2017.8.1 - 2017.8.1
  5. // desc: 程序更新工具
  6. // Copyright (c) yuzhengyang. All rights reserved.
  7. //************************************************************************
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Diagnostics;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Text;
  14. using Y.Utils.DataUtils.GuidUtils;
  15. using Y.Utils.DelegateUtils;
  16. using Y.Utils.IOUtils.FileUtils;
  17. using Y.Utils.IOUtils.PathUtils;
  18. using Y.Utils.NetUtils.FTPUtils;
  19. using Y.Utils.NetUtils.HttpUtils;
  20. using Y.Utils.VersionUtils;
  21. namespace Y.Utils.UpdateUtils
  22. {
  23. /// <summary>
  24. /// 程序更新工具
  25. /// </summary>
  26. public class AppUpdateTool
  27. {
  28. /// <summary>
  29. /// 获取新版本
  30. /// </summary>
  31. /// <param name="url"></param>
  32. /// <param name="version"></param>
  33. /// <param name="info"></param>
  34. /// <returns>
  35. /// -10;//请求版本失败
  36. /// -20;//没有更新的版本
  37. /// </returns>
  38. public int GetNewVersion(string url, Version version, out AppUpdateInfo info)
  39. {
  40. Stopwatch stopwatch = new Stopwatch();
  41. stopwatch.Start();
  42. info = HttpTool.Get<AppUpdateInfo>(url);
  43. if (info != null)
  44. {
  45. Version newVersion = VersionTool.Format(info.Version);
  46. if (newVersion != null && newVersion > version)
  47. {
  48. stopwatch.Stop();
  49. return (int)stopwatch.Elapsed.TotalSeconds;//成功返回操作时长
  50. }
  51. else
  52. {
  53. return -20;//没有更新的版本
  54. }
  55. }
  56. return -10;//请求版本失败
  57. }
  58. /// <summary>
  59. /// 更新
  60. /// </summary>
  61. /// <param name="info">新版本信息</param>
  62. /// <param name="tempPath">文件下载位置</param>
  63. /// <param name="dictionary">文件相对位置字典</param>
  64. /// <param name="downProgress">下载进度回调</param>
  65. /// <param name="downSender">下载进度事件数据</param>
  66. /// <param name="releaseProgress">释放进度回调</param>
  67. /// <param name="releaseSender">释放进度事件数据</param>
  68. /// <returns>
  69. /// -10000;//没有新版本
  70. /// -20000;//文件下载失败
  71. /// -30000;//文件释放失败
  72. /// </returns>
  73. public int Update(AppUpdateInfo info, string tempPath, Dictionary<string, string> dictionary,
  74. ProgressDelegate.ProgressHandler downProgress = null, object downSender = null,
  75. ProgressDelegate.ProgressHandler releaseProgress = null, object releaseSender = null)
  76. {
  77. Stopwatch stopwatch = new Stopwatch();
  78. stopwatch.Start();
  79. //请求最新版本信息
  80. if (info != null)
  81. {
  82. string file = DirTool.Combine(tempPath, GuidTool.Short() + "-" + info.Version);
  83. //准备更新(下载)
  84. string downfile = Download(file, info, downProgress, downSender);
  85. if (!string.IsNullOrWhiteSpace(downfile) && File.Exists(downfile))
  86. {
  87. //格式化释放文件目录(相对路径转换为绝对路径)
  88. string releasepath = AppDirTool.Get(info.ReleasePath, dictionary);
  89. //释放文件,释放完成后删除临时文件
  90. int unpackCode = FilePackageTool.Unpack(downfile, releasepath, releaseProgress, releaseSender);
  91. File.Delete(file);
  92. if (unpackCode > 0)
  93. {
  94. stopwatch.Stop();
  95. return (int)stopwatch.Elapsed.TotalSeconds;
  96. }
  97. else
  98. {
  99. return -30000 + unpackCode;//文件释放失败
  100. }
  101. }
  102. else
  103. {
  104. return -20000;//文件下载失败
  105. }
  106. }
  107. else
  108. {
  109. return -10000;//没有新版本
  110. }
  111. }
  112. private string Download(string file, AppUpdateInfo info, ProgressDelegate.ProgressHandler progress = null, object sender = null)
  113. {
  114. if (info != null)
  115. {
  116. switch (info.DownloadMode)
  117. {
  118. case 0://http 下载
  119. {
  120. if (HttpTool.Download(info.HttpUrl, file, progress, sender))
  121. return file;
  122. }
  123. break;
  124. case 1://ftp 下载
  125. {
  126. FtpTool ftp = new FtpTool(info.FtpIp, info.FtpAccount, info.FtpPassword);
  127. if (ftp.Download(info.FtpFile, file, progress, sender))
  128. return file;
  129. }
  130. break;
  131. }
  132. }
  133. return null;
  134. }
  135. }
  136. }