本文共 3950 字,大约阅读时间需要 13 分钟。
GeneralUpdate 是一款基于 .NET Framework 开发的自动升级程序,适用于 WPF、控制台应用和 WinForm 开发项目。它的核心设计目标是将更新逻辑抽象化,便于多种应用场景使用。与传统方法不同,GeneralUpdate 不需要深入了解源码,可以直接通过 Nuget 包管理,极大提升了开发效率。
using GeneralUpdate.Core;using GeneralUpdate.Core.Models;// 示例配置var args = new string[6] { "0.0.0.0", "1.1.1.1", "https://github.com/WELL-E", "http://192.168.50.225:7000/update.zip", "E:PlatformPath", "509f0ede227de4a662763a4abe3d8470"};// 创建 bootstrap 实例var bootstrap = new GeneralUpdateBootstrap();// 注册事件处理bootstrap.DownloadStatistics += OnDownloadStatistics;bootstrap.ProgressChanged += OnProgressChanged;// 配置更新策略bootstrap.Strategy() .Option(UpdateOption.Format, "zip") // 指定更新包格式 .Option(UpdateOption.MainApp, "YourApplicationName") // 指定更新完成后启动的主程序 .Option(UpdateOption.DownloadTimeOut, 60) // 下载超时时间(默认30秒)// 启动更新bootstrap.RemoteAddress(args) .Launch(); using GeneralUpdate.Single;using System.Windows;// 确保只运行一个实例const string appId = "{7F280539-0814-4F9C-95BF-D2BB60023657}";public partial class App : Application, ISingleInstanceApp{ [STAThread] protected override void OnStartup(StartupEventArgs e) { if (e.Args == null || e.Args.Length == 0) { // 示例参数配置 var args = new string[6] { "0.0.0.0", "1.1.1.1", "https://github.com/WELL-E", "http://192.168.50.225:7000/update.zip", "E:PlatformPath", "509f0ede227de4a662763a4abe3d8470" }; } else { // 从命令行获取参数 var args = e.Args; } if (args?.Length != 6) return; if (SingleInstance.InitializeAsFirstInstance(appId)) { var mainWindow = new MainWindow(); var viewModel = new MainViewModel(args, mainWindow.Close); mainWindow.DataContext = viewModel; var app = new App(); app.InitializeComponent(); app.Run(mainWindow); SingleInstance.Cleanup(); } } public bool SignalExternalCommandLineArgs(List args) { if (mainWindow.WindowState == WindowState.Minimized) mainWindow.WindowState = WindowState.Normal; mainWindow.Activate(); return true; }} using System;using System.Diagnostics;using System.ComponentModel;namespace MyProcessSample{ class MyProcess { void OpenApplication(string myFavoritesPath) { Process.Start("IExplore.exe"); Process.Start(myFavoritesPath); } void OpenWithArguments() { Process.Start("IExplore.exe", "www.northwindtraders.com"); Process.Start("IExplore.exe", "C:\myPath\myFile.htm"); Process.Start("IExplore.exe", "C:\myPath\myFile.asp"); } void OpenWithStartInfo() { var startInfo = new ProcessStartInfo("IExplore.exe"); startInfo.WindowStyle = ProcessWindowStyle.Minimized; Process.Start(startInfo); startInfo.Arguments = "www.northwindtraders.com"; Process.Start(startInfo); } static void Main() { var myFavoritesPath = Environment.GetFolderPath(Environment.SpecialFolder.Favorites); var myProcess = new MyProcess(); myProcess.OpenApplication(myFavoritesPath); myProcess.OpenWithArguments(); myProcess.OpenWithStartInfo(); } }} A1:只要不是框架级别的更新都可以直接更新。将最终版本的包发布到服务器即可,无需处理增量更新,直接覆盖即可。
A2:不是,GeneralUpdate 是独立程序,可单独运行。
A3:目前不支持,但计划未来开发。
A4:通过覆盖本地客户端文件实现。
转载地址:http://kvkbz.baihongyu.com/