🏝

.NET9 MAUIでWindowsアプリケーションを1ファイルで発行する

に公開

.NET MAUIでWindowsアプリケーションを配布したい場合に、Windowsストアに登録することなく、発行するための記事が見当たらなかったので、備忘録としてここに残します。

まず、プロジェクトファイルの設定は、以下の通りです。

HogeHogeApp.csproj
<Project Sdk="Microsoft.NET.Sdk">
	<PropertyGroup>
		<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">
			$(TargetFrameworks);net9.0-windows10.0.19041.0
		</TargetFrameworks>

		<OutputType>Exe</OutputType>
		<RootNamespace>HogeHogeApp</RootNamespace>
		<UseMaui>true</UseMaui>
		<SingleProject>true</SingleProject>
		<ImplicitUsings>enable</ImplicitUsings>
		<Nullable>enable</Nullable>

		<!-- Display name -->
		<ApplicationTitle>HogeHogeApp</ApplicationTitle>

		<!-- App Identifier -->
		<ApplicationId>com.company.HogeHogeApp</ApplicationId>

		<!-- Versions -->
		<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
		<ApplicationVersion>1</ApplicationVersion>

		<!-- To develop, package, and publish an app to the Microsoft Store, see:
		https://aka.ms/MauiTemplateUnpackaged -->
		<!-- 以下の設定が、exeファイル発行に必須である -->
		<WindowsPackageType>None</WindowsPackageType>

		<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">
			10.0.17763.0
		</SupportedOSPlatformVersion>
		<TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">
			10.0.17763.0
		</TargetPlatformMinVersion>
	</PropertyGroup>

	<ItemGroup>
		<!-- App Icon -->
		<MauiIcon Include="Resources\AppIcon\appicon.svg" ForegroundFile="Resources\AppIcon\appiconfg.svg" Color="#512BD4" />

		<!-- Splash Screen -->
		<MauiSplashScreen Include="Resources\Splash\splash.svg" Color="#512BD4" BaseSize="128,128" />

		<!-- Images -->
		<MauiImage Include="Resources\Images\*" />
		<MauiImage Update="Resources\Images\dotnet_bot.png" Resize="True" BaseSize="300,185" />

		<!-- Custom Fonts -->
		<MauiFont Include="Resources\Fonts\*" />

		<!-- Raw Assets (also remove the "Resources\Raw" prefix) -->
		<MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />
	</ItemGroup>

    <!-- プロジェクトファイルと関連がない、ファイルを含めたい場合に設定する -->
	<ItemGroup>
		<Content Include="fugafuga\**" CopyToPublishDirectory="PreserveNewest" />
	</ItemGroup>

	<ItemGroup>
		<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
		<PackageReference Include="Microsoft.Maui.Controls" Version="9.0.82" />
		<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="9.0.7" />
		<!-- 以下のパッケージファイルがexe発行に必須なので、NuGetパッケージ管理で以下をインストールする -->
		<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.26100.4188" />
		<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.7.250606001" />
	</ItemGroup>

</Project>

プロジェクトファイルの設定で重要なのは、WindowsPackageTypeタグでNoneとすることと、NuGetパッケージでWindows.SDK.BuildToolsWindowsAppSDKをインストールすることです。

次に、以下のコマンドで、exeファイルを発行します。

dotnet publish HogeHogeApp.csproj -f net9.0-windows10.0.19041.0 -c Release -p:RuntimeIdentifierOverride=win10-x64 -p:WindowsPackageType=None -p:WindowsAppSDKSelfContained=true -p:PublishSingleFile=true

上記のコマンドの結果、以下の場所にexeファイルが生成されます。

bin\x64\Release\net9.0-windows10.0.19041.0\win10-x64\publish

Discussion