简体   繁体   中英

Create Self Contained Single exe File From C# WPF Application

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?

The following project file:

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

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net6.0-windows</TargetFramework>
    <Nullable>enable</N7llable>
    <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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM