.NET Framework 4.5 及以上集成 Squirrel 打包应用的方法
1. 官网#
https://github.com/Squirrel/Squirrel.Windows
2. 集成到项目#
在 NuGet 包管理器中添加 Squirrel.Windows 到项目,注意项目需基于 .NET Framework 4.5 以上
在需要检测更新的位置添加如下代码:
using (var mgr = new UpdateManager("C:\\Projects\\MyApp\\Releases"))
{
await mgr.UpdateApp();
}UpdateManager 接受本地路径和网络路径,实际使用中应为更新服务器地址
3. 打包#
官方示例是使用 NuGet Package Manager 手动制作 nupkg 文件,并在最后提供了 VS 自动生成的方案。
以下为自动生成方案:
在 .csproj 文件添加如下内容:
<Target Name="AfterBuild" Condition=" '$(Configuration)' == 'Release'">
<GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
<Output TaskParameter="Assemblies" ItemName="myAssemblyInfo"/>
</GetAssemblyIdentity>
<Exec Command="nuget pack MyApp.nuspec -Version %(myAssemblyInfo.Version) -Properties Configuration=Release -OutputDirectory $(OutDir) -BasePath $(OutDir)" />
<Exec Command="squirrel --releasify $(OutDir)MyApp.$([System.Version]::Parse(%(myAssemblyInfo.Version)).ToString(3)).nupkg" />
</Target>其中 MyApp.nuspec 内容如下:
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>MyApp</id>
<!-- version will be replaced by MSBuild -->
<version>0.0.0.0</version>
<title>title</title>
<authors>authors</authors>
<description>description</description>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<copyright>Copyright 2016</copyright>
<language>zh-CN</language>
<dependencies />
</metadata>
<files>
<file src="*.*" target="lib\net45\" exclude="*.pdb;*.nupkg;*.vshost.*"/>
</files>
</package>注意 MyApp.nuspec 与 .csproj 在同一目录下。
以上完成之后将解决方案配置切换到 Release 下,并重新构建,就会生成安装包。
如果失败请参考一下步骤:
检查 nuget 命令是否可用,如果不可用则通过下面的命令安装:
PM> Install-Package NuGet.CommandLine如果安装后还有问题可以关闭 VS 并重新打开工程试试。
4. 分发与安装#
将 Releases 文件夹下内容上传到服务器,并将 Setup.exe 提供给首次安装的用户下载与安装。
已经安装过的用户会在更新完成后再次启动生效。