Windows 程式安裝與更新方案: Clowd.Squirrel

来源:https://www.cnblogs.com/ohzxc/archive/2022/05/21/16295615.html
-Advertisement-
Play Games

我的Notion Clowd.Squirrel Squirrel.Windows 是一組工具和適用於.Net的庫,用於管理 Desktop Windows 應用程式的安裝和更新。 Squirrel.Windows 對 Windows 應用程式的實現語言沒有任何要求,甚至無需服務端即可完成增量更新。 ...


我的Notion

Clowd.Squirrel

Squirrel.Windows 是一組工具和適用於.Net的庫,用於管理 Desktop Windows 應用程式的安裝和更新。 Squirrel.Windows 對 Windows 應用程式的實現語言沒有任何要求,甚至無需服務端即可完成增量更新。

Clowd.Squirrel 是 Squirrel.Windows 的一個優秀分支。2019 年 Squirrel.Windows 宣佈不再維護,雖然 2020 年又重新恢復維護,但其不再處於積極開發階段,依賴庫開始陳舊。所以推薦轉移到 Clowd.Squirrel,用法也更加簡單。

快速使用

下麵以 .net 程式 和 vs 2022 為例,介紹如何使用 Clowd.Squirrel

  1. 安裝 Clowd.Squirrel

    1. 通過 nuget包管理器安裝 Clowd.Squirrel,

    2. 安裝後,目錄 ..\packages\Clowd.Squirrel.2.9.40\tools 里是用到的工具

  2. 創建文件 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>
    

  3. 在程式啟動入口增加檢查更新相關代碼

    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!");
    
    	// 啟動你的應用
    }
    
  4. 版本號改成3段,需要符合SemVer規範

    [assembly: AssemblyVersion("1.3.2")]
    [assembly: AssemblyFileVersion("1.3.2")]
    
  5. .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>
    

Squirrel.exe 參數

Squirrel pack`
 --releaseDir .\Release             # 更新輸出到該目錄
 --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伺服器,供用戶下載

後續更新

代碼稍作修改後,提高版本號,再次編譯多出以下文件

其中delta是相交於1.3.16的增量更新包,將RELEASES delta文件發到web伺服器,UpdateManager類從該web伺服器地址獲取RELEASES,檢查是否有更新,

你也可以再將Setup.exe文件發到web伺服器覆蓋舊的Setup.exe,以便新安裝用戶都能下載到最新的安裝包

撤回更新

如果不小心發佈了問題包。修改bug後,提高版本號,編譯。

刪除RELEASES文件中有問題的包信息,

發佈full 和RELEASES,以便後續用戶能更新到正常版本

快捷方式

根據下列順序,第一個不為空的,作為快捷方式名稱

  1. [assembly: AssemblyProduct("MyApp") (AssemblyInfo.cs)
  2. Squirrel.exe packTitle 參數
  3. [assembly: AssemblyDescription("MyApp") ( AssemblyInfo.cs)
  4. exe 文件名

這裡我使用 packTitle ,方便控制Release與Test用不同的名稱打包。

改進 .csproj 項目文件 內容

$(SolutionDir)packages\Clowd.Squirrel.2.9.40\tools\Squirrel.exe pack  --packTitle 我的APP$(Configuration) --packId $(Configuration).$(ProjectName) --packVersion $([System.Version]::Parse(%(myAssemblyInfo.Version)).ToString(3)) --packAuthors 作者 --packDirectory $(OutDir) --releaseDir .\Publish\$(Configuration) --icon $(ProjectDir)logo.ico

user.config 問題

如果你的應用也使用user.config,會出現”更新版本後設置丟失,變成預設設置“的問題。根本原因是新版 exe 和舊版 exe 目錄不同。

user.config 保存在

%LocalAppData%\公司名\MyApp.exe_[Url|StrongName]_Hash碼\版本號\user.config

例如

C:\Users\yourname\AppData\Local\yourcompany\MyApp.exe_Url_qdx0no02b2yzg0ddn33isevehzmexfmy\1.3.4.0\user.config

其中Hash碼是根據exe所在目錄,exe名稱等計算所得

而 Squirrel 更新會產生一個新的 app-版本號 文件夾,導致 user.config 目錄變化,舊版本的用戶設置在新版上不生效

搜索一番解決方法比較複雜,例如重寫一個設置適配器SettingsProvider

我的思路

在設置目錄里查找,把MyApp.exe_Url_或MyApp.exe_StrongName_開頭的文件夾,把低版本的user.config設置複製過來就行了

具體的代碼邏輯

wpf

//檢查本地配置文件夾
var configPath = GetDefaultExeConfigPath(ConfigurationUserLevel.PerUserRoamingAndLocal);
var configName = "user.config";
var exeName = Assembly.GetExecutingAssembly().GetName().Name + ".exe";
var companyDirectoryName = configPath.Split(new string[1] { exeName }, StringSplitOptions.RemoveEmptyEntries)[0];
var companyDirectory = new DirectoryInfo(companyDirectoryName);
if (companyDirectory.Exists)
{
    configPath = configPath.TrimEnd((@"\" + configName).ToCharArray());
    configPath = configPath.TrimEnd((@"\" + Assembly.GetExecutingAssembly().GetName().Version).ToCharArray());

    var configDirectory = new DirectoryInfo(configPath);
    if (!configDirectory.Exists)
    {

        var urltargetName = exeName + "_Url_";//非強簽名Url
        var strongNametargetName = exeName + "_StrongName_";//強簽名StrongName

        var drs = companyDirectory.GetDirectories();
        var theDrs = drs.Where(x => x.Name.StartsWith(urltargetName)).Concat(drs.Where(x => x.Name.StartsWith(strongNametargetName)));
        if (theDrs.Count() > 0)
        {
            configDirectory.Create();
            foreach (var theDr in theDrs)
            {
                foreach (var d in theDr.GetDirectories())
                {
                    CopyDirectory(d.FullName, configDirectory.FullName + @"\" + d, true);
                }
            }
        }
    }
}

//最後,把低版本配置升級到最新版。
//新版本號下是否有user.config,如果沒有從舊版本升級配置
if (!ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).HasFile)
{
    Settings.Default.Upgrade();
}

winfrom(exeName 和 Version 獲取方式和 wpf 不一樣)

//檢查本地配置文件夾
var configPath = Config.GetDefaultExeConfigPath(ConfigurationUserLevel.PerUserRoamingAndLocal);
var configName = "user.config";
var exeName = ResourceAssembly.GetName().Name + ".exe";
var companyDirectoryName = configPath.Split(new string[1] { exeName }, StringSplitOptions.RemoveEmptyEntries)[0];
var companyDirectory = new DirectoryInfo(companyDirectoryName);
if (companyDirectory.Exists)
{
    configPath = configPath.TrimEnd((@"\" + configName).ToCharArray());
    configPath = configPath.TrimEnd((@"\" + ResourceAssembly.GetName().Version.ToString()).ToCharArray());
    var configDirectory = new DirectoryInfo(configPath);
    if (!configDirectory.Exists)
    {

        var urltargetName = exeName + "_Url_";//非強簽名Url
        var strongNametargetName = exeName + "_StrongName_";//強簽名StrongName

        var drs = companyDirectory.GetDirectories();
        var theDrs = drs.Where(x => x.Name.StartsWith(urltargetName)).Concat(drs.Where(x => x.Name.StartsWith(strongNametargetName)));
        if (theDrs.Count() > 0)
        {
            configDirectory.Create();
            foreach (var theDr in theDrs)
            {
                foreach (var d in theDr.GetDirectories())
                {
                    CopyDirectory(d.FullName, configDirectory.FullName + @"\" + d, true);
                }
            }
        }
    }
}

//最後,把低版本配置升級到最新版。
//新版本號下是否有user.config,如果沒有從舊版本升級配置
if (!ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).HasFile)
{
    Print.Properties.Settings.Default.Upgrade();
    Settings.Default.Upgrade();
}
static void CopyDirectory(string sourceDir, string destinationDir, bool recursive)
{
    var dir = new DirectoryInfo(sourceDir);
    if (!dir.Exists)
        return;
    DirectoryInfo[] dirs = dir.GetDirectories();
    Directory.CreateDirectory(destinationDir);
    foreach (FileInfo file in dir.GetFiles())
    {
        string targetFilePath = Path.Combine(destinationDir, file.Name);
        if (!new FileInfo(destinationDir + @"\" + file.Name).Exists)
        {
            file.CopyTo(targetFilePath, true);
        }
    }

    if (recursive)
    {
        foreach (DirectoryInfo subDir in dirs)
        {
            string newDestinationDir = Path.Combine(destinationDir, subDir.Name);
            CopyDirectory(subDir.FullName, newDestinationDir, true);
        }
    }
}
static string GetDefaultExeConfigPath(ConfigurationUserLevel userLevel)
{
    try
    {
        var UserConfig = ConfigurationManager.OpenExeConfiguration(userLevel);
        return UserConfig.FilePath;
    }
    catch (ConfigurationException e)
    {
        return e.Filename;
    }
}

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 強轉int類型會直接對浮點數的小數部分進行截斷(無論是正還是負)。還有一種方法是math.ceil和math.floor。無論是正數還是負數,都遵循:ceil往數軸正方向取整,floor往數軸負方向取整。round原型為round(value, ndigits),可以將一個浮點數取整到固定的小數位。... ...
  • 很多人都喜歡使用黑色的主題樣式,包括我自己,使用了差不多三年的黑色主題,但是個人覺得在進行視窗轉換的時候很廢眼睛。 比如IDEA是全黑的,然後需要看PDF或者WORD又變成白色的了,這樣來回切換導致眼睛很累,畢竟現在網頁以及大部分軟體的界面都是白色的。那麼還是老老實實的使用原來比較順眼的模式吧。 1 ...
  • 本期教程人臉識別第三方平臺為虹軟科技,本文章講解的是人臉識別RGB活體追蹤技術,免費的功能很多可以自行搭配,希望在你看完本章課程有所收穫。 ...
  • 《零基礎學Java》 繪製幾何圖形 Java可以分別使用 Graphics 和 Graphics2D 繪製圖形,Graphics類 使用不同的方法繪製不同的圖形(drawLine()方法可f以繪製線、drawRect()方法用於繪製矩形、drawOval()方法用於繪製橢圓形)。 Graphics類 ...
  • 我使用Spring AOP實現了用戶操作日誌功能 今天答辯完了,復盤了一下系統,發現還是有一些東西值得拿出來和大家分享一下。 需求分析 系統需要對用戶的操作進行記錄,方便未來溯源 首先想到的就是在每個方法中,去實現記錄的邏輯,但是這樣做肯定是不現實的,首先工作量大,其次違背了軟體工程設計原則(開閉原 ...
  • 大家好,今天給大家介紹一款輕量、快速、穩定可編排的組件式規則引擎框架LiteFlow。 一、LiteFlow的介紹 LiteFlow官方網站和代碼倉庫地址 官方網站:https://yomahub.com/liteflow Gitee托管倉庫:https://gitee.com/dromara/li ...
  • 1. Netty源碼研究筆記(3)——Channel系列 依舊是通過先縱向再橫向的研究方法,在開篇中,我們發現不管是Sever還是Client,最終的啟動是通過調用channel的對應方法來完成的,而這個動作實際在channel綁定的eventLoop中執行。 接下來,我們繼續EchoSever、E ...
  • 轉載請註明來源 https://www.cnblogs.com/brucejiao/p/16188865.html 謝謝! 轉載請註明來源 https://www.cnblogs.com/brucejiao/p/16188865.html 謝謝! 轉載請註明來源 https://www.cnblog ...
一周排行
    -Advertisement-
    Play Games
  • Dapr Outbox 是1.12中的功能。 本文只介紹Dapr Outbox 執行流程,Dapr Outbox基本用法請閱讀官方文檔 。本文中appID=order-processor,topic=orders 本文前提知識:熟悉Dapr狀態管理、Dapr發佈訂閱和Outbox 模式。 Outbo ...
  • 引言 在前幾章我們深度講解了單元測試和集成測試的基礎知識,這一章我們來講解一下代碼覆蓋率,代碼覆蓋率是單元測試運行的度量值,覆蓋率通常以百分比表示,用於衡量代碼被測試覆蓋的程度,幫助開發人員評估測試用例的質量和代碼的健壯性。常見的覆蓋率包括語句覆蓋率(Line Coverage)、分支覆蓋率(Bra ...
  • 前言 本文介紹瞭如何使用S7.NET庫實現對西門子PLC DB塊數據的讀寫,記錄了使用電腦模擬,模擬PLC,自至完成測試的詳細流程,並重點介紹了在這個過程中的易錯點,供參考。 用到的軟體: 1.Windows環境下鏈路層網路訪問的行業標準工具(WinPcap_4_1_3.exe)下載鏈接:http ...
  • 從依賴倒置原則(Dependency Inversion Principle, DIP)到控制反轉(Inversion of Control, IoC)再到依賴註入(Dependency Injection, DI)的演進過程,我們可以理解為一種逐步抽象和解耦的設計思想。這種思想在C#等面向對象的編 ...
  • 關於Python中的私有屬性和私有方法 Python對於類的成員沒有嚴格的訪問控制限制,這與其他面相對對象語言有區別。關於私有屬性和私有方法,有如下要點: 1、通常我們約定,兩個下劃線開頭的屬性是私有的(private)。其他為公共的(public); 2、類內部可以訪問私有屬性(方法); 3、類外 ...
  • C++ 訪問說明符 訪問說明符是 C++ 中控制類成員(屬性和方法)可訪問性的關鍵字。它們用於封裝類數據並保護其免受意外修改或濫用。 三種訪問說明符: public:允許從類外部的任何地方訪問成員。 private:僅允許在類內部訪問成員。 protected:允許在類內部及其派生類中訪問成員。 示 ...
  • 寫這個隨筆說一下C++的static_cast和dynamic_cast用在子類與父類的指針轉換時的一些事宜。首先,【static_cast,dynamic_cast】【父類指針,子類指針】,兩兩一組,共有4種組合:用 static_cast 父類轉子類、用 static_cast 子類轉父類、使用 ...
  • /******************************************************************************************************** * * * 設計雙向鏈表的介面 * * * * Copyright (c) 2023-2 ...
  • 相信接觸過spring做開發的小伙伴們一定使用過@ComponentScan註解 @ComponentScan("com.wangm.lifecycle") public class AppConfig { } @ComponentScan指定basePackage,將包下的類按照一定規則註冊成Be ...
  • 操作系統 :CentOS 7.6_x64 opensips版本: 2.4.9 python版本:2.7.5 python作為腳本語言,使用起來很方便,查了下opensips的文檔,支持使用python腳本寫邏輯代碼。今天整理下CentOS7環境下opensips2.4.9的python模塊筆記及使用 ...