WPF程序打包工具 Velopack (开源)
1.在主项目中安装 Velopack NuGet 包:Install the Velopack NuGet Package in your main project:
dotnet add package Velopack
2.在以下开头配置 Velopack 应用程序:Program.Main
需要在 .csproj 后缀的文件中 添加
<StartupObject>xxx.Program</StartupObject>
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>WinExe</OutputType> <TargetFramework>net48</TargetFramework> <ImplicitUsings>true</ImplicitUsings> <UseWPF>true</UseWPF> </PropertyGroup> <PropertyGroup> <!-- This overrides the default Program.Main that WPF creates for you, and allows you to add VelopackApp --> <StartupObject>VeloWpfSample.Program</StartupObject> </PropertyGroup> <ItemGroup> <!--Condition below is only needed to test this sample project against the local projects instead of the NuGet package. Remove it in your app.--> <PackageReference Include="Velopack" Version="0.*" Condition="$(UseLocalVelopack) == ''" /> </ItemGroup> </Project>
public class Program { public static MemoryLogger Log { get; private set; } [STAThread] public static void Main(string[] args) { try { VelopackApp.Build() .WithFirstRun((v) => { MessageBox.Show("Thanks for installing my application!"); }) .Run(); // We can now launch the WPF application as normal. var app = new App(); app.InitializeComponent(); app.Run(); } catch (Exception ex) { MessageBox.Show("Unhandled exception: " + ex.ToString()); } } }
3.将自动更新添加到您的应用
private static async Task UpdateMyApp() { var mgr = new UpdateManager("https://the.place/you-host/updates"); // check for new version var newVersion = await mgr.CheckForUpdatesAsync(); if (newVersion == null) return; // no update available // download new version await mgr.DownloadUpdatesAsync(newVersion); // install new version and restart app mgr.ApplyUpdatesAndRestart(newVersion); }
4.安装命令行工具:vpk
dotnet tool update -g vpk
5.发布 dotnet 并构建您的第一个 Velopack 版本!
dotnet publish -c Release --self-contained -r win-x64 -o .\publish vpk pack -u YourAppId -v 1.0.0 -p .\publish -e yourMainApp.exe
yourMainApp.exe 生成的程序运行文件名称
YourAppId 安装包的名称