Squirrel.Windows 是一组工具和适用于.Net的库,用于管理 Desktop Windows 应用程序的安装和更新。 Squirrel.Windows 对 Windows 应用程序的实现语言没有任何要求,甚至无需服务端即可完成增量更新。
Clowd.Squirrel 是 Squirrel.Windows 的一个优秀分支。2019 年 Squirrel.Windows 宣布不再维护,虽然 2020 年又重新恢复维护,但其不再处于积极开发阶段,依赖库开始陈旧。所以推荐转移到 Clowd.Squirrel,用法也更加简单。
下面以 .net 程序 和 vs 2022 为例,介绍如何使用 Clowd.Squirrel
安装 Clowd.Squirrel
通过 nuget包管理器安装 Clowd.Squirrel,
安装后,目录 ..\packages\Clowd.Squirrel.2.9.40\tools 里是用到的工具
创建文件 Properties\app.manifest,并在项目属性→生成→设置清单设置该文件
这一步是为了指定该项目exe需要创建快捷方式,否则安装时会将所有exe文件都建立一个快捷方式
<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<SquirrelAwareVersion xmlns="urn:schema-squirrel-com:asm.v1">1</SquirrelAwareVersion>
</assembly>
在程序启动入口增加检查更新相关代码
public static void Main(string[] args)
{
// run Squirrel first, as the app may exit after these run
SquirrelAwareApp.HandleEvents(
onInitialInstall: OnAppInstall,
onAppUninstall: OnAppUninstall,
onEveryRun: OnAppRun);
//本地文件夹或服务器地址
using (var mgr = new UpdateManager(@"D:\\Desktop\\test"))
{
var newVersion = await mgr.UpdateApp();
// optionally restart the app automatically, or ask the user if/when they want to restart
if (newVersion != null)
{
UpdateManager.RestartApp();
}
}
// ... other app init code after ...
}
private static void OnAppInstall(SemanticVersion version, IAppTools tools)
{
tools.CreateShortcutForThisExe(ShortcutLocation.StartMenu | ShortcutLocation.Desktop);
}
private static void OnAppUninstall(SemanticVersion version, IAppTools tools)
{
tools.RemoveShortcutForThisExe(ShortcutLocation.StartMenu | ShortcutLocation.Desktop);
}
private static void OnAppRun(SemanticVersion version, IAppTools tools, bool firstRun)
{
tools.SetProcessAppUserModelId();
// show a welcome message when the app is first installed
if (firstRun) MessageBox.Show("Thanks for installing my application!");
// 启动你的应用
}
版本号改成3段,需要符合SemVer规范
[assembly: AssemblyVersion("1.3.2")]
[assembly: AssemblyFileVersion("1.3.2")]
.csproj 项目文件增加下面的代码,编译 Release 时自动打包
<Target Name="AfterReleaseBuild" AfterTargets="AfterBuild" Condition=" '$(Configuration)' == 'Release'">
<GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
<Output TaskParameter="Assemblies" ItemName="myAssemblyInfo" />
</GetAssemblyIdentity>
<Exec Command="$(SolutionDir)packages\\Clowd.Squirrel.2.9.40\\tools\\Squirrel.exe pack --packId $(ProjectName) --packVersion $([System.Version]::Parse(%(myAssemblyInfo.Version)).ToString(3)) --packAuthors XXX --packDirectory $(OutDir)" />
</Target>
releaseDir 更新输出到该目录
Squirrel pack`
--framework net6,vcredist143-x86` # Install .NET 6.0 (x64) and vcredist143 (x86) during setup, if not installed
--packId "YourApp"` # Application / package name
--packTitle "YourApp"` # Application / package name
--packVersion "1.0.0"` # Version to build. Should be supplied by your CI
--packAuthors "YourCompany"` # Your name, or your company name
--packDirectory ".\\publish"` # The directory the application was published to
--icon "mySetupIcon.ico"` # Icon for Setup.exe
--splashImage "install.gif" # The splash artwork (or animation) to be shown during install
切换Release模式,编译产生
exe 用于首次安装,先将它发到web服务器,供用户下载