从 C# WPF 应用程序创建独立的单个 exe 文件

问题描述 投票:0回答:1

我正在尝试为这个基本的小应用程序创建一个可分发的可执行文件:

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();
    }
  }
}

我的 csproj 文件包含:

<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>

但是,当我运行

dotnet publish -r Release
时,我在
Release
文件夹中得到了数十个文件,而不是一个exe文件。如果我只从该文件夹中取出 exe 文件并将其放在其他位置,它就不会运行,那么显然还需要其他几十个文件。

在类似问题的旧答案中,推荐的方法是使用 ilmerge,但是,现在已弃用。是否有替代方案或者不再可能创建独立的 exe 文件?

c# wpf exe single-file .net-core-publishsinglefile
1个回答
2
投票

以下项目文件:

<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>

...以及以下命令...:

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

...可用于创建独立的单文件部署 WPF 应用程序。

© www.soinside.com 2019 - 2024. All rights reserved.