AppUpdateTool.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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="name">功能名称</param>
  30. /// <param name="version">当前版本号</param>
  31. /// <param name="url">请求新版本地址</param>
  32. /// <param name="path">文件下载位置</param>
  33. /// <param name="dictionary">文件相对位置字典</param>
  34. /// <param name="downprogress">下载进度回调</param>
  35. /// <param name="downsender">下载进度事件数据</param>
  36. /// <param name="releaseprogress">释放进度回调</param>
  37. /// <param name="releasesender">释放进度事件数据</param>
  38. /// <returns>
  39. /// -10;//无最新版本,停止操作
  40. /// -20;//请求服务器最新版本失败
  41. /// -30;//新版本号格式不正确,解析失败
  42. /// -40;//文件下载失败
  43. /// -50;//文件释放失败
  44. /// </returns>
  45. public int Update(string name, Version version, string url, string path, Dictionary<string, string> dictionary,
  46. ProgressDelegate.ProgressHandler downprogress = null, object downsender = null,
  47. ProgressDelegate.ProgressHandler releaseprogress = null, object releasesender = null)
  48. {
  49. Stopwatch stopwatch = new Stopwatch();
  50. stopwatch.Start();
  51. //请求最新版本信息
  52. AppUpdateInfo info = HttpTool.Get<AppUpdateInfo>(string.Format("{0}?name={1}", url, name));
  53. if (info != null)
  54. {
  55. Version newVersion = GerVersion(info.Version);
  56. if (newVersion == null) return -30;//新版本号格式不正确,解析失败
  57. if (newVersion > version)
  58. {
  59. string file = DirTool.Combine(path, name + newVersion.ToString());
  60. //准备更新(下载)
  61. string downfile = Download(file, info, downprogress, downsender);
  62. if (!string.IsNullOrWhiteSpace(downfile) && File.Exists(downfile))
  63. {
  64. //格式化释放文件目录
  65. string releasepath = AppDirTool.Get(info.ReleasePath, dictionary);
  66. //释放文件
  67. if (FilePackageTool.Unpack(downfile, releasepath, releaseprogress, releasesender) > 0)
  68. {
  69. stopwatch.Stop();
  70. return (int)stopwatch.Elapsed.TotalSeconds;
  71. }
  72. else
  73. {
  74. return -50;//文件释放失败
  75. }
  76. }
  77. else
  78. {
  79. return -40;//文件下载失败
  80. }
  81. }
  82. else
  83. {
  84. return -10;//无最新版本,停止操作
  85. }
  86. }
  87. else
  88. {
  89. return -20;//请求服务器最新版本失败
  90. }
  91. }
  92. private Version GerVersion(string s)
  93. {
  94. //解析最新版本号
  95. Version version = null;
  96. try
  97. {
  98. version = new Version(s);
  99. }
  100. catch (Exception e) { }
  101. return version;
  102. }
  103. private string Download(string file, AppUpdateInfo info, ProgressDelegate.ProgressHandler progress = null, object sender = null)
  104. {
  105. FtpTool ftp = new FtpTool(info.FtpIp, info.FtpAccount, info.FtpPassword);
  106. if (ftp.Download(info.FtpFile, file, progress, sender))
  107. return file;
  108. return null;
  109. }
  110. }
  111. }