Create Self Contained Single exe File From C# WPF Application

1.8k views Asked by At

I'm trying to create a single exe distributable of this basic little application:

using System;
using System.Windows;
namespace BlackScreen
{
  public partial class App : Application
  {
    [STAThread]
    public static void Main()
    {
      App app = new App();
      Window window = new Window();
      window.Show();
      app.MainWindow = window;
      app.Run();
    }
  }
}

My csproj file contains:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net7.0-windows</TargetFramework>
    <Nullable>enable</Nullable>
    <UseWPF>true</UseWPF>

    <PublishSingleFile>true</PublishSingleFile>
    <IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract>
    <IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
  </PropertyGroup>

</Project>

However, when I run dotnet publish -r Release I get dozens of files in the Release folder, not a single exe file. If I take just the exe file out of that folder and place it somewhere else it doesn't run, so clearly those dozens of other files are required.

In older answers to similar questions the recommended approach is to use ilmerge, however, that has now been deprecated. Is there an alternative or is creating a self contained exe file no longer possible?

1

There are 1 answers

0
mm8 On

The following project file:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net6.0-windows</TargetFramework>
    <Nullable>enable</Nullable>
    <UseWPF>true</UseWPF>
    <IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
  </PropertyGroup>

</Project>

...and the following command...:

dotnet publish -c Release -r win-x64 --self-contained -p:PublishSingleFile=true -p:PublishReadyToRun=true

...can be used to create a self-contained and single-file deployment WPF app.


Info: This provided command is still working with .net-8 and WPF-application in self-contained publish.