1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

WPFの配布と更新について_InnoSetupとAutoUpdater.NET

Last updated at Posted at 2025-03-12

WPFアプリを作り、それをインストーラーとして配布して、バージョンアップがあった時は、更新するようにしたい。
Visual Studioだとmsixがありますが、VSCodeだと手間そうなので他の方法を試してみました。

こちらの方の記事で、AutoUpdater.NETを知り試してみました!
.NET9のWPFアプリを爆速で作る アップデート編

準備

  • .NET9 vscode
  • Inno Setup ver6.4.1

WPFアプリ

とりあえず適当なWPFアプリを作ります。
その時にバージョン情報を入れておきます。
これを入れておかないと、バージョン情報で更新するかどうかの判定ができなくなるため。

<PropertyGroup><Version>0.3.4.0</Version>
</PropertyGroup>
  • バージョン情報
    image.png

Inno Setupの変更箇所

おおよそはウィザードでの初期表示そのままですが、以下を変更してみました。

1. スタートアップに追加

TasksとIconsに追記することで、スタートアップへ追加するチェックボタンを表示できる。
アプリを常駐させたい時とかに使える。

[Tasks]
Name: "startupicon"; Description: "スタートアップにショートカットを作成";

[Icons]
Name: "{userstartup}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: startupicon

これだとWarningがでるので[Setup]PrivilegesRequired=noneを追記しておきます。

Warning: The [Setup] section directive "PrivilegesRequired" is set to "admin" but per-user areas (userstartup) are used by the script. Regardless of the version of Windows, if the installation is running in administrative install mode then you should be careful about making any per-user area changes: such changes may not achieve what you are intending. See the "UsedUserAreasWarning" topic in help file for more information.

2. 規定でチェックを入れる

  • デスクトップ・スタートアップへのショートカットのTasksの所でFlags: uncheckedを消すと規定でレ点が入ります
  • checkedなのかなと思ったけどそんなのなかった

3. インストーラーの出力フォルダをissと同じ場所にする

  • [Setup]でOutputDir=.に変更する

4. インストーラーへのバージョン情報追加

  • [Setup]にVersionInfoVersion={#MyAppVersion}を追加する

5. アプリとインストーラーのバージョンを合わせる

  • アプリ側で変更してインストーラーの方で間違いそうなので以下で対応
  • アプリのバージョン情報が1.1.1.1のように「.」区切りの4つの数値でないと取得できないかもしれません
; パスは""ではなく''で囲む
#define MyAppPath 'mypath\path\'
#define MyAppVersion GetVersionNumbersString(MyAppPath + MyAppExeName)

[Setup]
AppVersion={#MyAppVersion}
VersionInfoVersion={#MyAppVersion}

AutoUpdater.NET

こちらの方の記事を参考にさせて頂きました。
.NET9のWPFアプリを爆速で作る アップデート編

  • dotnet add package Autoupdater.NET.Officialで追加

アプリとxmlのバージョンを合わせる

アップデートはアプリ自体のバージョン情報とUpdateInfo.xmlのバージョン情報で比較されるので、この変更を間違ったらアップデートが入らなくなってしまいます。
ビルド時にバージョンを合わせるようにしてみます。

  1. 別でクラスライブラリをつくる

    • dotnet new classlib -n VersionSyncTask
    • cd VersionSyncTask
    • dotnet add package Microsoft.Build.Framework
    • dotnet add package Microsoft.Build.Utilities.Core
    VersionSyncTask
    VersionSync.cs
    using System.Xml.Linq;
    using Microsoft.Build.Framework;
    
    namespace VersionSyncTask
    {
        public class VersionSync : Microsoft.Build.Utilities.Task
        {
            [Required]
            public string CsprojPath { get; set; } = "";
    
            [Required]
            public string XmlPath { get; set; } = "";
    
            public override bool Execute()
            {
                try
                {
                    // .csprojファイルのバージョンを取得
                    XDocument csprojDoc = XDocument.Load(CsprojPath);
                    var versionElement = csprojDoc?.Root?.Element("PropertyGroup")?.Element("Version");
                    string version = versionElement!.Value;
    
                    // UpdateInfo.xmlファイルのバージョンを更新
                    XDocument xmlDoc = XDocument.Load(XmlPath);
                    var xmlVersionElement = xmlDoc.Root?.Element("version");
                    xmlVersionElement!.Value = version;
    
                    // UpdateInfo.xmlファイルを保存
                    xmlDoc.Save(XmlPath);
    
                    Log.LogMessage(MessageImportance.High, "バージョンが同期されました: " + version);
                    return true;
                }
                catch (Exception ex)
                {
                    Log.LogErrorFromException(ex);
                    return false;
                }
            }
        }
    }
    
    • CsprojPathXmlPathはcsprojから呼び出す時に指定します
    • dotnet publish -c Release
  2. ビルドしたdllをwpfのプロジェクトへ保存する

  3. UpdateInfo.xmlをwpfプロジェクトへ保存する

  4. csprojへtaskを追記する

    • これでプロジェクトのバージョン情報とxmlを合わせることができました
      <!-- バージョンの同期 -->
      <UsingTask TaskName="VersionSyncTask.VersionSync" AssemblyFile="VersionSync\VersionSyncTask.dll" />
      <Target Name="SyncVersion" BeforeTargets="BeforeBuild">
        <VersionSync CsprojPath="$(MSBuildProjectFile)" XmlPath="VersionSync\UpdateInfo.xml" />
      </Target>
    
  5. 更新したxmlファイルをビルドフォルダへコピーする

    <ItemGroup><!-- ビルド時にコピーするファイル -->
        <None Update="VersionSync\UpdateInfo.xml">
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </None>
    </ItemGroup>
    

インストーラーの発行

インストーラーへのコンパイルもできるように以下をつくっておきます。
コマンドパレットから、Tasks: Run Taskでつくったのを実行します。

tasks.json
"tasks": [
    
    {
        "label": "compile-inno-setup",
        "type": "shell",
        "command": "C:\\Program Files (x86)\\Inno Setup 6\\ISCC.exe",
        "args": [
            "C:\\Users\\path\\app.iss"
        ],
    }
]

参考

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?