AutoUpdater.NET 客户端自定更新文件

AutoUpdater.NET 客户端更新指定文件,具体使用如下

1、新建项目windows窗体项目--WindowsFormTestUpdate

2、添加NuGet程序管理包

      选择项目右键,选择“管理NuGet程序包”,搜索“Autoupdater.NET.Official”,选择,点击安装

3、窗体代码如下:

界面

代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using AutoUpdaterDotNET;
using Newtonsoft.Json;

namespace WindowsFormTestUpdate
{
    public partial class TestUpdateForm : Form
    {
        public TestUpdateForm()
        {
            InitializeComponent();
        }

        private void TestUpdateForm_Load(object sender, EventArgs e)
        {
            lblVersion.Text = "版本:" + Application.ProductName + Application.ProductVersion;
        }

        private void btnAutoUpdate_Click(object sender, EventArgs e)
        {
            AutoUpdater.ReportErrors = true;
            //1、根据服务端的更新文件update.xml的version检测是否与当前应用程序版本一致,是否需要更新。
            //AutoUpdater.Start("http://localhost:8082/TestAutoUpdate/update.xml");

            //2、根据服务端的更新文件update.xml的version检测是否与指定myAssembly应用程序版本一致,是否需要更新。
            //AutoUpdater.Start("http://rbsoft.org/updates/AutoUpdaterTest.xml", System.Reflection.Assembly.GetExecutingAssembly()/*this.GetType().Assembly*/);

            //3、根据FTP服务端的更新文件update.xml的version检测是否与指定myAssembly应用程序版本一致,是否需要更新。
            //AutoUpdater.Start("ftp://127.0.0.1/TestAutoUpdate/update.xml", new NetworkCredential("myuser", "mypassword"));

            //其他属性设置
            //设置更新弹窗的大小
            //AutoUpdater.UpdateFormSize = new System.Drawing.Size(800, 600);

            AutoUpdater.AppCastURL = "";//服务端的更新文件update.xml
            AutoUpdater.InstalledVersion = new Version("1.3");
            //AutoUpdater.AppTitle = "更新弹窗";//弹出窗体标题
            AutoUpdater.DownloadPath = "";//升级下载包路径
            AutoUpdater.PersistenceProvider = "";//升级下载包路径
            AutoUpdater.Mandatory = true;
            AutoUpdater.Mandatory = true;
            AutoUpdater.UpdateMode = Mode.Forced;
            //AutoUpdater.ShowRemindLaterButton = true;//显示稍后提醒按钮
            AutoUpdater.LetUserSelectRemindLater = false;
            AutoUpdater.RemindLaterTimeSpan = RemindLaterFormat.Days;
            AutoUpdater.RemindLaterAt = 2;

            //AutoUpdater.ShowSkipButton = true;//显示稍后提醒按钮
            //AutoUpdater.Synchronous = true;//显示稍后提醒按钮
            var proxy = new WebProxy("ProxyIP:ProxyPort", true)
            {
                Credentials = new NetworkCredential("ProxyUserName", "ProxyPassword")
            };
            AutoUpdater.Proxy = proxy;


            设置凭证
            BasicAuthentication basicAuthentication = new BasicAuthentication("myUserName", "myPassword");
            AutoUpdater.BasicAuthXML = AutoUpdater.BasicAuthDownload = AutoUpdater.BasicAuthChangeLog = basicAuthentication;

            浏览器打开下载
            AutoUpdater.OpenDownloadPage = true;

            //string jsonPath = Path.Combine(Environment.CurrentDirectory, "settings.json");
            //AutoUpdater.PersistenceProvider = new JsonFilePersistenceProvider(jsonPath);


            Handling Application exit logic manually,自定义更新成功之后操作
            //AutoUpdater.ApplicationExitEvent += AutoUpdater_ApplicationExitEvent;
            //自定义更新
            //AutoUpdater.CheckForUpdateEvent += AutoUpdaterOnCheckForUpdateEvent;

            //自定义更新文件转换,可以指定json类型的文件,如下
            //AutoUpdater.ParseUpdateInfoEvent += AutoUpdaterOnParseUpdateInfoEvent;
            //AutoUpdater.Start("http://localhost:8082/AutoUpdaterTest.json");

            #region 指定json类型的文件
            //         {
            //             "version":"2.0.0.0",
            //"url":"http://rbsoft.org/downloads/AutoUpdaterTest.zip",
            //"changelog":"https://github.com/ravibpatel/AutoUpdater.NET/releases",
            //"mandatory":{
            //                 "value":true,
            //   "minVersion": "2.0.0.0",
            //   "mode":1
            //},
            //"checksum":{
            //                 "value":"E5F59E50FC91A9E52634FFCB11F32BD37FE0E2F1",
            //   "hashingAlgorithm":"SHA1"
            //}
            //         }

            #endregion

            //Text = Application.StartupPath;
            AutoUpdater.Start("http://localhost:8082/TestAutoUpdate/update.xml");
        }
        private void AutoUpdater_ApplicationExitEvent()
        {
            //Text = @"Closing application...";
            Thread.Sleep(5000);
            //Application.Exit();
            //string exepath = @"C:\Users\Administrator\Desktop\Debug\AutoUpdaterTest";
            //var currentDirectory = new DirectoryInfo(Application.StartupPath);
            //if (currentDirectory.Parent != null)
            //{
            //    exepath = Path.Combine(currentDirectory.Parent.FullName, "TestAutoUpdate", "WindowsFormTestUpdate.exe");
            //}
            
            //lblVersion.Text = lblVersion.Text + $"----{exepath}";
            //System.Diagnostics.Process.Start(exepath);
        }


        private void AutoUpdaterOnCheckForUpdateEvent(UpdateInfoEventArgs args)
        {
            if (args.Error == null)
            {
                if (args.IsUpdateAvailable)
                {
                    DialogResult dialogResult;
                    if (args.Mandatory.Value)
                    {
                        dialogResult =
                            MessageBox.Show(
                                $@"There is new version {args.CurrentVersion} available. You are using version {args.InstalledVersion}. This is required update. Press Ok to begin updating the application.", @"Update Available",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
                    }
                    else
                    {
                        dialogResult =
                            MessageBox.Show(
                                $@"There is new version {args.CurrentVersion} available. You are using version {
                                        args.InstalledVersion
                                    }. Do you want to update the application now?", @"Update Available",
                                MessageBoxButtons.YesNo,
                                MessageBoxIcon.Information);
                    }

                    // Uncomment the following line if you want to show standard update dialog instead.
                    // AutoUpdater.ShowUpdateForm(args);

                    if (dialogResult.Equals(DialogResult.Yes) || dialogResult.Equals(DialogResult.OK))
                    {
                        try
                        {
                            if (AutoUpdater.DownloadUpdate(args))
                            {
                                Application.Exit();
                            }
                        }
                        catch (Exception exception)
                        {
                            MessageBox.Show(exception.Message, exception.GetType().ToString(), MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                        }
                    }
                }
                else
                {
                    MessageBox.Show(@"There is no update available please try again later.", @"No update available",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                if (args.Error is WebException)
                {
                    MessageBox.Show(
                        @"There is a problem reaching update server. Please check your internet connection and try again later.",
                        @"Update Check Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    MessageBox.Show(args.Error.Message,
                        args.Error.GetType().ToString(), MessageBoxButtons.OK,
                        MessageBoxIcon.Error);
                }
            }
        }

        private void AutoUpdaterOnParseUpdateInfoEvent(ParseUpdateInfoEventArgs args)
        {
            dynamic json = JsonConvert.DeserializeObject(args.RemoteData);
            args.UpdateInfo = new UpdateInfoEventArgs
            {
                CurrentVersion = json.version,
                ChangelogURL = json.changelog,
                DownloadURL = json.url,
                Mandatory = new Mandatory
                {
                    Value = json.mandatory.value,
                    UpdateMode = json.mandatory.mode,
                    MinimumVersion = json.mandatory.minVersion
                },
                CheckSum = new CheckSum
                {
                    Value = json.checksum.value,
                    HashingAlgorithm = json.checksum.hashingAlgorithm
                }
            };
        }
    }
}

注意:

1、 获取配置文件方式可以是网站服务器,也可以是ftp服务器,配置文件可以是xml的,当然也可以是其他格式的,但是其他格式的,如json,获取方式就不一样了(AutoUpdater.Start("http://localhost:8082/AutoUpdaterTest.json"))

一般是使用如下:

//1、根据服务端的更新文件update.xml的version检测是否与当前应用程序版本一致,是否需要更新。
AutoUpdater.Start("http://localhost:8082/TestAutoUpdate/update.xml");

//2、根据服务端的更新文件update.xml的version检测是否与指定myAssembly应用程序版本一致,是否需要更新。
AutoUpdater.Start("http://rbsoft.org/updates/AutoUpdaterTest.xml", System.Reflection.Assembly.GetExecutingAssembly()/*this.GetType().Assembly*/);

//3、根据FTP服务端的更新文件update.xml的version检测是否与指定myAssembly应用程序版本一致,是否需要更新。
AutoUpdater.Start("ftp://127.0.0.1/TestAutoUpdate/update.xml", new NetworkCredential("myuser", "mypassword"));

2、程序集版本(不同版本的程序集可以通过程序集版本来控制,以示区别)

4、运行结果

 5、其他的一些配置可以参考:

https://github.com/ravibpatel/AutoUpdater.NET

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Electron 应用自动更新通常是通过使用 Electron 提供的 `autoUpdater` 模块来完成的。该模块提供了一种简单的方法来检查更新和下载更新。以下是一些步骤来实现自动更新: 1. 在应用程序的 `main` 进程中,使用 `autoUpdater` 模块来检查更新。您可以使用 `setFeedURL` 方法指定更新的源。 ```javascript const { app, autoUpdater } = require('electron') autoUpdater.setFeedURL('http://your-update-server.com') autoUpdater.checkForUpdates() ``` 2. 在您的更新服务器上,您需要提供一个更新文件,该文件包含了应用程序的最新版本信息和更新文件的下载链接。您可以使用任何服务器端技术来生成此文件,例如 PHP 或 Node.js。 ```json { "version": "1.2.3", "url": "http://your-update-server.com/updates/your-app-1.2.3.dmg" } ``` 3. 当 `autoUpdater` 模块检测到新版本时,它会触发一个 `update-available` 事件。在该事件的处理程序中,您可以向用户显示更新的可用性,并询问他们是否要下载和安装更新。 ```javascript autoUpdater.on('update-available', () => { // Show dialog to user and ask if they want to update }) ``` 4. 如果用户同意更新,您可以使用 `autoUpdater` 模块下载更新文件并安装它。在下载期间,`autoUpdater` 模块将触发 `progress` 事件,以便您可以向用户显示下载进度。 ```javascript autoUpdater.on('update-downloaded', () => { autoUpdater.quitAndInstall() }) autoUpdater.on('progress', (progressObj) => { // Show progress to user }) ``` 这些是实现 Electron 应用自动更新的基本步骤。您可以根据您的应用程序的需求进行定制。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值