• WinForm、Wpf自动升级 AutoUpdater.NET


    Github

    AutoUpdater.NET

    目录

    一、IIS部署 更新站点

    二、创建Winform


    一、IIS部署 更新站点

    IIS默认站点目录下创建 目录 Downloads、Updates

    Updates目录创建文件 UpdateLog.html、AutoUpdaterStarter.xml

    UpdateLog.html:

    1. <html>
    2. <body>
    3. <h1>
    4. UpDate
    5. h1>
    6. body>
    7. html>

     AutoUpdaterStarter.xml:

    url节点 为下载更新的地址 http://127.0.0.1/Downloads/fr.zip

    1. "UTF-8"?>
    2. <item>
    3. <version>1.0.0.2version>
    4. <url>http://127.0.0.1/Downloads/fr.zipurl>
    5. <changelog>http://127.0.0.1/Updates/UpdateLog.htmlchangelog>
    6. item>

    二、创建Winform

    netcore 3.1 + Winform 

    nuget安装包

    Autoupdater.NET.Official        --版本1.8.0

     2.1创建MainForm

    http://127.0.0.1/Updates/AutoUpdaterStarter.xml 为IIS站点更新配置文件

    检查版本

    1. public partial class MainForm : Form
    2. {
    3. public MainForm()
    4. {
    5. InitializeComponent();
    6. Assembly assembly = Assembly.GetEntryAssembly();
    7. label1.Text = $"{assembly.GetName().Version}";//显示版本号
    8. AutoUpdatorHelper.Start("http://127.0.0.1/Updates/AutoUpdaterStarter.xml", this);
    9. }
    10. private void button1_Click(object sender, EventArgs e)
    11. {
    12. AutoUpdater.Start("http://127.0.0.1/Updates/AutoUpdaterStarter.xml");//手动更新
    13. }
    14. public class AutoUpdatorHelper
    15. {
    16. ///
    17. /// 自动更新
    18. ///
    19. ///
    20. ///
    21. public static void Start(string serverPath, ISynchronizeInvoke synchronizeInvoke)
    22. {
    23. #region 每隔60秒检查一次更新(判断依据是AssemblyInfo中的版本和xml文件的版本是否一致,如果服务器xml文件的版本大于AssemblyInfo中的版本则触发CheckForUpdateEvent)
    24. System.Timers.Timer timer = new System.Timers.Timer
    25. {
    26. Interval = 60 * 1000,//毫秒
    27. SynchronizingObject = synchronizeInvoke
    28. };
    29. timer.Elapsed += (object sender, ElapsedEventArgs e) =>
    30. {
    31. AutoUpdater.Start(serverPath, Assembly.GetExecutingAssembly());
    32. };
    33. timer.Start();
    34. #endregion
    35. AutoUpdater.LetUserSelectRemindLater = true;
    36. AutoUpdater.RemindLaterTimeSpan = RemindLaterFormat.Minutes;
    37. AutoUpdater.RemindLaterAt = 1;
    38. //若您不想在更新表单上显示“跳过”按钮,那个么只需在上面的代码中添加以下行即可。
    39. AutoUpdater.ShowSkipButton = false;
    40. //如果要同步检查更新,请在启动更新之前将Synchronous设置为true,如下所示。
    41. AutoUpdater.Synchronous = true;
    42. //若你们不想在更新表单上显示“以后提醒”按钮,那个么只需在上面的代码中添加以下一行即可。
    43. AutoUpdater.ShowRemindLaterButton = false;
    44. //如果要忽略先前设置的“以后提醒”和“跳过”设置,则可以将“强制”属性设置为true。它还将隐藏“跳过”和“稍后提醒”按钮。如果在代码中将强制设置为true,那么XML文件中的强制值将被忽略。
    45. AutoUpdater.Mandatory = false;
    46. //您可以通过添加以下代码来打开错误报告。如果执行此自动更新程序。NET将显示错误消息,如果没有可用的更新或无法从web服务器获取XML文件。
    47. AutoUpdater.ReportErrors = true;
    48. //如果服务器xml文件的版本大于AssemblyInfo中的版本则触发CheckForUpdateEvent
    49. AutoUpdater.CheckForUpdateEvent += (args) =>
    50. {
    51. if (args.Error == null)
    52. {
    53. //检测到有可用的更新
    54. if (args.IsUpdateAvailable)
    55. {
    56. DialogResult dialogResult;
    57. if (args.Mandatory.Value)
    58. {
    59. dialogResult =
    60. MessageBox.Show(
    61. $@"当前有一个新版本{args.CurrentVersion}可用.你正在使用版本{args.InstalledVersion}.点击确认开始更新", @"更新可用",
    62. MessageBoxButtons.OK,
    63. MessageBoxIcon.Information);
    64. }
    65. else
    66. {
    67. dialogResult =
    68. MessageBox.Show(
    69. $@"当前有一个新版本{args.CurrentVersion}可用.你正在使用版本{args.InstalledVersion}.确认要更新吗?", @"更新可用",
    70. MessageBoxButtons.YesNo,
    71. MessageBoxIcon.Information);
    72. }
    73. if (dialogResult.Equals(DialogResult.Yes) || dialogResult.Equals(DialogResult.OK))
    74. {
    75. try
    76. {
    77. //触发更新下载
    78. if (AutoUpdater.DownloadUpdate(args))
    79. {
    80. Application.Exit();
    81. }
    82. }
    83. catch (Exception exception)
    84. {
    85. MessageBox.Show(exception.Message, exception.GetType().ToString(), MessageBoxButtons.OK,
    86. MessageBoxIcon.Error);
    87. }
    88. }
    89. }
    90. else
    91. {
    92. MessageBox.Show(
    93. $@"当前为最新新版本", @"更新可用",
    94. MessageBoxButtons.OK,
    95. MessageBoxIcon.Information);
    96. }
    97. }
    98. else
    99. {
    100. if (args.Error is WebException)
    101. {
    102. MessageBox.Show(
    103. @"连接更新服务器失败,请检查网络连接.",
    104. @"更新检查失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
    105. }
    106. else
    107. {
    108. MessageBox.Show(args.Error.Message,
    109. args.Error.GetType().ToString(), MessageBoxButtons.OK,
    110. MessageBoxIcon.Error);
    111. }
    112. }
    113. };
    114. }
    115. }
    116. }

     2.2打包

    winfrom生成文件添加到压缩文件 fr.zip,复制到IIS站点Downloads目录下

     

    2.3更新

     手动更新

     自动更新从版本1.0.0.1 更新到1.0.0.2 

  • 相关阅读:
    2023icpc网络预选赛I. Pa?sWorD(dp)
    django——Serializer的反序列化、字段与参数、局部与全局钩子、ModelSerializer使用
    Pytorch框架学习记录4——数据集的使用(torchvision.dataset)
    软件测试V模型
    Spring Cloud Alibaba Nacos注册中心(单机)
    创业合伙必读之:合伙企业登记指南
    【编译部署】使用Visual Studio编译Linux平台程序/动态库(远程连接)
    3. Nginx 核心功能
    Java实现防重复提交,使用自定义注解的方式
    MySQL进阶 —— 超详细操作演示!!!(上)
  • 原文地址:https://blog.csdn.net/czjnoe/article/details/136389343