AppUpdateTool.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.DelegateUtils;
  15. using Y.Utils.IOUtils.FileUtils;
  16. using Y.Utils.IOUtils.PathUtils;
  17. using Y.Utils.NetUtils.FTPUtils;
  18. using Y.Utils.NetUtils.HttpUtils;
  19. namespace Y.Utils.UpdateUtils
  20. {
  21. /// <summary>
  22. /// 程序更新工具
  23. /// </summary>
  24. public class AppUpdateTool
  25. {
  26. /// <summary>
  27. /// 获取新版本
  28. /// </summary>
  29. /// <param name="url"></param>
  30. /// <param name="name"></param>
  31. /// <param name="version"></param>
  32. /// <param name="info"></param>
  33. /// <returns>
  34. /// -10;//请求版本失败
  35. /// -20;//没有更新的版本
  36. /// </returns>
  37. public int GetNewVersion(string url, Version version, out AppUpdateInfo info)
  38. {
  39. Stopwatch stopwatch = new Stopwatch();
  40. stopwatch.Start();
  41. info = HttpTool.Get<AppUpdateInfo>(url);
  42. if (info != null)
  43. {
  44. Version newVersion = FormatVersion(info.Version);
  45. if (newVersion != null && newVersion > version)
  46. {
  47. stopwatch.Stop();
  48. return (int)stopwatch.Elapsed.TotalSeconds;//成功返回操作时长
  49. }
  50. else
  51. {
  52. return -20;//没有更新的版本
  53. }
  54. }
  55. return -10;//请求版本失败
  56. }
  57. /// <summary>
  58. /// 更新
  59. /// </summary>
  60. /// <param name="info">新版本信息</param>
  61. /// <param name="tempPath">文件下载位置</param>
  62. /// <param name="dictionary">文件相对位置字典</param>
  63. /// <param name="downProgress">下载进度回调</param>
  64. /// <param name="downSender">下载进度事件数据</param>
  65. /// <param name="releaseProgress">释放进度回调</param>
  66. /// <param name="releaseSender">释放进度事件数据</param>
  67. /// <returns>
  68. /// -10000;//没有新版本
  69. /// -20000;//文件下载失败
  70. /// -30000;//文件释放失败
  71. /// </returns>
  72. public int Update(AppUpdateInfo info, string tempPath, Dictionary<string, string> dictionary,
  73. ProgressDelegate.ProgressHandler downProgress = null, object downSender = null,
  74. ProgressDelegate.ProgressHandler releaseProgress = null, object releaseSender = null)
  75. {
  76. Stopwatch stopwatch = new Stopwatch();
  77. stopwatch.Start();
  78. //请求最新版本信息
  79. if (info != null)
  80. {
  81. string file = DirTool.Combine(tempPath, info.Name + info.Version);
  82. //准备更新(下载)
  83. string downfile = Download(file, info, downProgress, downSender);
  84. if (!string.IsNullOrWhiteSpace(downfile) && File.Exists(downfile))
  85. {
  86. //格式化释放文件目录(相对路径转换为绝对路径)
  87. string releasepath = AppDirTool.Get(info.ReleasePath, dictionary);
  88. //释放文件
  89. int unpackCode = 0;
  90. if ((unpackCode = FilePackageTool.Unpack(downfile, releasepath, releaseProgress, releaseSender)) > 0)
  91. {
  92. stopwatch.Stop();
  93. return (int)stopwatch.Elapsed.TotalSeconds;
  94. }
  95. else
  96. {
  97. return -30000 + unpackCode;//文件释放失败
  98. }
  99. }
  100. else
  101. {
  102. return -20000;//文件下载失败
  103. }
  104. }
  105. else
  106. {
  107. return -10000;//没有新版本
  108. }
  109. }
  110. private Version FormatVersion(string s)
  111. {
  112. //解析最新版本号
  113. Version version = null;
  114. try
  115. {
  116. version = new Version(s);
  117. }
  118. catch (Exception e) { }
  119. return version;
  120. }
  121. private string Download(string file, AppUpdateInfo info, ProgressDelegate.ProgressHandler progress = null, object sender = null)
  122. {
  123. FtpTool ftp = new FtpTool(info.FtpIp, info.FtpAccount, info.FtpPassword);
  124. if (ftp.Download(info.FtpFile, file, progress, sender))
  125. return file;
  126. return null;
  127. }
  128. }
  129. }