AppUpdateTool.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. /// -10000;//无最新版本,停止操作
  40. /// -20000;//请求服务器最新版本失败
  41. /// -30000;//新版本号格式不正确,解析失败
  42. /// -40000;//文件下载失败
  43. /// -50000;//文件释放失败
  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 -30000;//新版本号格式不正确,解析失败
  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. int unpackCode = 0;
  68. if ((unpackCode = FilePackageTool.Unpack(downfile, releasepath, releaseprogress, releasesender)) > 0)
  69. {
  70. stopwatch.Stop();
  71. return (int)stopwatch.Elapsed.TotalSeconds;
  72. }
  73. else
  74. {
  75. return -50000 + unpackCode;//文件释放失败
  76. }
  77. }
  78. else
  79. {
  80. return -40000;//文件下载失败
  81. }
  82. }
  83. else
  84. {
  85. return -10000;//无最新版本,停止操作
  86. }
  87. }
  88. else
  89. {
  90. return -20000;//请求服务器最新版本失败
  91. }
  92. }
  93. private Version GerVersion(string s)
  94. {
  95. //解析最新版本号
  96. Version version = null;
  97. try
  98. {
  99. version = new Version(s);
  100. }
  101. catch (Exception e) { }
  102. return version;
  103. }
  104. private string Download(string file, AppUpdateInfo info, ProgressDelegate.ProgressHandler progress = null, object sender = null)
  105. {
  106. FtpTool ftp = new FtpTool(info.FtpIp, info.FtpAccount, info.FtpPassword);
  107. if (ftp.Download(info.FtpFile, file, progress, sender))
  108. return file;
  109. return null;
  110. }
  111. }
  112. }