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
  • 概述:這個WPF項目通過XAML繪製汽車動態速度表盤,實現了0-300的速度刻度,包括數字、指針,並通過定時器模擬速度變化,展示了動態效果。詳細實現包括界面設計、刻度繪製、指針角度計算等,通過C#代碼與XAML文件結合完成。 新建 WPF 項目: 在 Visual Studio 中創建一個新的 WP ...
  • 概述:在WPF中使用`WpfAnimatedGif`庫展示GIF動畫,首先確保全裝了該庫。通過XAML設置Image控制項,指定GIF路徑,然後在代碼中使用庫提供的方法實現動畫控制。這簡化了在WPF應用中處理GIF圖的過程,提供了方便的介面來管理動畫播放和暫停。 當使用 WpfAnimatedGif  ...
  • 您是否曾經訪問過一個網站,它需要很長時間載入,最終你敲擊 F5 重新載入頁面。 即使用戶刷新了瀏覽器取消了原始請求,而對於伺服器來說,API也不會知道它正在計算的值將在結束時被丟棄,刷新五次,伺服器將觸發 5 個請求。 為瞭解決這個問題,ASP.NET Core 為 Web 伺服器提供了一種機制,就 ...
  • 本章將和大家分享如何通過 Elasticsearch 實現自動補全查詢功能。 一、自動補全-安裝拼音分詞器 1、自動補全需求說明 當用戶在搜索框輸入字元時,我們應該提示出與該字元有關的搜索項,如圖: 2、使用拼音分詞 要實現根據字母做補全,就必須對文檔按照拼音分詞。在 GitHub 上恰好有 Ela ...
  • using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Windows.Forms; namespace OOP { pub ...
  • 概述:以上內容詳細介紹了在C#中如何從另一個線程更新GUI,包括基礎功能和高級功能。對於WinForms,使用`Control.Invoke`;對於WPF,使用`Dispatcher.Invoke`。高級功能使用`SynchronizationContext`實現線程間通信,確保清晰、可讀性高的代碼 ...
  • Nuget包 Microsoft.Extensions.Telemetry.Abstractions 包含的新的日誌記錄source generator,它支持使用[LogProperties]將整個對象作為State與日誌一起記錄。 我將展示一種方法來控制如何使用[LogProperties]對象 ...
  • 支持.Net/.Net Core/.Net Framework,可以部署在Docker, Windows, Linux, Mac。 常見的ORM技術(比如:Entity Framework,Dapper,SqlSugar,NHibernate,等…),它們不是在做Sql語句的程式化變種,就是在做Sq ...
  • 一、引言 在現代應用程式開發中,尤其是在涉及I/O操作(如網路請求、文件讀寫等)時,非同步編程成為了提高性能和用戶體驗的關鍵技術。C#作為.NET框架下的主流開發語言,提供了強大的非同步編程支持,通過async/await關鍵字,可以讓開發者以同步的方式編寫非同步代碼,極大地簡化了非同步編程的複雜性。本文將 ...
  • 一、引言 在.NET開發中,操作Office文檔(特別是Excel和Word)是一項常見的需求。然而,在伺服器端或無Microsoft Office環境的場景下,直接使用Office Interop可能會面臨挑戰。為瞭解決這個問題,開源庫NPOI應運而生,它提供了無需安裝Office即可創建、讀取和 ...