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
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...