# AutoUpdateSystem **Repository Path**: h3308855831/AutoUpdateSystem ## Basic Information - **Project Name**: AutoUpdateSystem - **Description**: 一个实用的 Windows 桌面应用程序自动更新框架,支持增量更新、断点续传、版本管理和自动回滚。 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-06-25 - **Last Updated**: 2026-06-25 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # AutoUpdateSystem - 自动更新系统 一个实用的 Windows 桌面应用程序自动更新框架,支持增量更新、断点续传、版本管理和自动回滚。 ## 功能特性 - **增量更新**:只下载有变化的文件,减少带宽消耗 - **MD5校验**:确保下载文件的完整性和安全性 - **自动回滚**:更新失败时自动恢复原版本 - **外部更新器**:支持更新正在运行的主程序 - **自动版本检测**:自动读取程序集版本号 - **灵活配置**:支持通过配置文件自定义所有参数 ## 项目结构 ``` AutoUpdateSystem/ ├── UpdateServer/ # 更新服务器(.NET 8 WebAPI) │ ├── Program.cs # 服务端主程序 │ ├── Models/ # 数据模型 │ └── wwwroot/ # 静态文件目录(产品发布目录) │ └── [ProductId]/ # 各产品的文件目录 │ ├── version.json # 版本清单(自动生成) │ ├── up.ini # 主程序配置(可选) │ └── *.exe, *.dll # 产品文件 │ ├── UpdateCore/ # 更新核心库(.NET Framework 4.7.2) │ ├── Utils/ │ │ ├── UpdateChecker.cs # 更新检测与执行引擎 │ │ └── UpdateLogger.cs # 日志记录 │ ├── Models/ # 数据模型 │ └── UpdateCore.csproj # 类库项目 │ ├── Updater/ # 外部更新器(WinForms) │ ├── Program.cs # 更新器入口 │ ├── UpdateForm.cs # 更新界面 │ └── Updater.csproj # WinForms项目 │ └── UpFrom/ # 客户端示例(WinForms) ├── Form1.cs # 示例主窗口 ├── upconfig.ini # 客户端配置文件 └── UpFrom.csproj # WinForms项目 ``` ## 快速开始 ### 1. 部署更新服务器 #### 编译发布 ```bash cd UpdateServer dotnet publish -c Release -r win-x64 --self-contained ``` #### 配置服务器 在发布目录创建 `server.ini` 配置文件: ```ini # 服务器配置文件 # 端口号(默认8080) Port=8080 # wwwroot目录路径(默认程序目录下的wwwroot) WwwRoot=wwwroot ``` #### 启动服务器 ```bash dotnet UpdateServer.dll ``` #### Windows 服务部署(推荐生产环境使用) **发布服务:** ```bash cd UpdateServer dotnet publish -c Release -r win-x64 --self-contained -o bin/Release/publish ``` **服务管理脚本(需要管理员权限):** > **重要**:`ServiceManager.ps1` 已配置为发布时自动复制到输出目录。如果你之前已经发布过,需要重新发布一次: > ```bash > cd UpdateServer > dotnet publish -c Release -r win-x64 --self-contained -o bin/Release/publish > ``` > 然后将 `bin/Release/publish` 整个目录复制到你的部署位置(如 `E:\server\updateserver\`)。 ```powershell # 进入你的部署目录(包含 UpdateServer.exe 和 ServiceManager.ps1) cd E:\server\updateserver # 安装服务(开机自启动 + 故障自动重启) .\ServiceManager.ps1 install # 其他操作 .\ServiceManager.ps1 start # 启动服务 .\ServiceManager.ps1 stop # 停止服务 .\ServiceManager.ps1 restart # 重启服务 .\ServiceManager.ps1 uninstall # 卸载服务 .\ServiceManager.ps1 status # 查看状态 ``` **服务特性:** - **开机自启动**:服务设置为 `auto` 启动类型,开机自动运行 - **故障自动重启**:服务失败后自动重启(5秒 → 10秒 → 30秒递进) - **心跳监控**:每5分钟写入 `service_heartbeat.log` 供监控检查 - **静默运行**:无控制台窗口,后台运行 ### 2. 发布产品更新 #### 方式一:自动检测(推荐) 将更新文件放入 `wwwroot/[ProductId]/` 目录,服务器会自动生成版本清单。 #### 方式二:手动指定版本 ```bash # 调用生成接口指定版本号 curl -X POST "http://localhost:8080/api/generate?productId=YourProduct&version=1.0.1" ``` #### 配置主程序(可选) 在产品目录下创建 `up.ini` 文件,明确指定主程序名称: ```ini # 产品配置 MainExe=YourProduct.exe ``` ### 3. 客户端集成 #### 引用更新核心库 ```bash # 添加 UpdateCore 项目引用 ``` #### 创建配置文件 在客户端程序目录下创建 `upconfig.ini`: ```ini # 客户端配置文件 # 更新服务器地址 ServerUrl=http://your-server:8080 # 产品标识(与服务端目录名一致) ProductId=YourProduct ``` #### 调用更新检查 ```csharp using UpdateCore.Utils; public partial class MainForm : Form { private UpdateChecker _updateChecker; public MainForm() { InitializeComponent(); // 从配置文件加载配置 LoadConfiguration(); // 初始化更新检查器 _updateChecker = new UpdateChecker(_serverUrl, _productId, Application.StartupPath); _updateChecker.ProgressChanged += OnProgressChanged; _updateChecker.StatusChanged += OnStatusChanged; _updateChecker.LogMessage += OnLogMessage; // 自动检查更新 Task.Run(async () => await CheckUpdateAsync()); } private async Task CheckUpdateAsync() { if (await _updateChecker.CheckForUpdatesAsync()) { var result = MessageBox.Show( $"发现新版本: {_updateChecker.RemoteManifest.Version}\n" + $"更新文件数: {_updateChecker.UpdateFiles.Count}\n\n是否立即更新?", "发现更新", MessageBoxButtons.YesNo, MessageBoxIcon.Information); if (result == DialogResult.Yes) { StartExternalUpdate(); } } } private void StartExternalUpdate() { _updateChecker.StartExternalUpdater("Updater.exe", "YourProduct.exe"); Environment.Exit(0); } } ``` ## API 接口说明 ### 版本检查 ``` GET /api/version?productId={productId} 响应示例: { "productId": "YourProduct", "productName": "YourProduct", "version": "1.0.1", "generateTime": "2024-01-15T10:30:00", "files": [ { "relativePath": "YourProduct.exe", "size": 17920, "md5": "439dcf2b9fb161afcb5093a3f881613c", "lastModified": "2024-01-15T10:00:00" } ] } ``` ### 文件下载 ``` GET /api/download?productId={productId}&file={relativePath} ``` ### 手动生成版本清单 ``` POST /api/generate?productId={productId}&version={version} ``` ### 获取产品列表 ``` GET /api/products 响应示例: ["YourProduct1", "YourProduct2"] ``` ### 健康检查 ``` GET /api/health ``` ## 配置文件说明 ### 服务端 server.ini | 配置项 | 说明 | 默认值 | |--------|------|--------| | Port | 监听端口 | 8080 | | WwwRoot | 产品文件目录 | 程序目录下的wwwroot | ### 客户端 upconfig.ini | 配置项 | 说明 | 必填 | |--------|------|------| | ServerUrl | 更新服务器地址 | 是 | | ProductId | 产品标识 | 是 | ### 产品 up.ini(可选) | 配置项 | 说明 | |--------|------| | MainExe | 主程序文件名 | ## 目录过滤规则 服务端在生成版本清单时,会自动跳过以下目录和文件: - `version.json` - 版本清单文件 - `UpdateBackup/` - 备份目录 - `UpdateLogs/` - 日志目录 - `up.ini` - 配置文件 - `upconfig.ini` - 客户端配置 - `.git/` - Git版本控制目录 ## 更新流程 1. **检查更新**:客户端向服务器请求版本清单 2. **对比版本**:比较本地版本与服务端版本 3. **下载文件**:从服务器下载需要更新的文件 4. **MD5校验**:验证下载文件的完整性 5. **备份文件**:更新前备份原文件 6. **应用更新**:替换旧文件 7. **完成更新**:启动主程序 ## 常见问题 ### Q: 服务端返回404? A: 请确保 `wwwroot/[ProductId]/` 目录下有文件,然后调用 `/api/generate` 生成版本清单。 ### Q: 版本号读取错误? A: 在产品目录下创建 `up.ini` 文件,指定 `MainExe=YourProduct.exe`。 ### Q: 文件被占用更新失败? A: 客户端使用外部更新器模式,调用 `StartExternalUpdater()` 后程序会自动退出。 ### Q: 如何指定自定义版本号? A: 调用生成接口时添加 `version` 参数:`/api/generate?productId=xxx&version=2.0.0` ### Q: 如何将服务端部署为 Windows 服务? A: 使用管理员权限运行 PowerShell,执行:`.\ServiceManager.ps1 install`。服务会开机自启动并配置故障自动重启。 ### Q: 服务安装失败,提示"找不到 UpdateServer.exe"? A: 请先编译发布项目:`dotnet publish -c Release -r win-x64 --self-contained -o bin/Release/publish` ### Q: 服务作为控制台程序运行和作为服务运行有什么区别? A: 功能完全一致。作为服务运行时无控制台窗口,日志前缀显示为 `[服务]`;控制台模式日志前缀显示为 `[控制台]`。 ## 技术栈 - **服务端**:.NET 8 + ASP.NET Core Minimal API - **客户端**:.NET Framework 4.7.2 + Windows Forms - **通信协议**:HTTP/JSON - **更新算法**:增量更新 + MD5校验 ## 许可证 Copyright (c) 2026 hmf MIT License