如何排除 App.config 被捆绑在 .net core 3 单文件自包含 WinForms 应用程序中的 .exe 中?

Car*_*ñoz 3 winforms .net-core .net-core-3.0

我有一个 Winforms .NET Core 3 应用程序,我想将其发布为独立的单文件部署

这是相关.csproj文件

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

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    <PublishSingleFile>true</PublishSingleFile>
  </PropertyGroup>

  <ItemGroup>
    <!--PackageReferences-->
  </ItemGroup>

  <ItemGroup>
    <!--ProjectReferences-->
  </ItemGroup>

</Project>
Run Code Online (Sandbox Code Playgroud)

我正在使用<RuntimeIdentifier>win-x64</RuntimeIdentifier>它,因此它为 Windows x64 生成一个自包含部署,<PublishSingleFile>true</PublishSingleFile>因此所有内容都嵌入到.exe文件中。

通过运行发布时:

dotnet publish -c Release
Run Code Online (Sandbox Code Playgroud)

我得到的.exe.pdb在文件bin\Release\netcoreapp3.0\win-x64\publish

- MyApp.exe
- MyApp.pdb
Run Code Online (Sandbox Code Playgroud)

我需要在.csproj文件中更改什么才能获得MyApp.dll.configMyApp.exe.config旁边正确的内容,.exe以便应用程序实际从中读取配置而不是其嵌入的配置App.Config

我试过添加这个

<ItemGroup>
  <Content Update="*.config">
    <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
    <ExcludeFromSingleFile>true</ExcludeFromSingleFile>
  </Content>
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)

正如此链接Single-file Publish - Build System Interface所暗示的那样,但它仍然只生成两个文件。

use*_*960 9

你的问题帮我解决了这个问题,谢谢。希望这也适用于您。

我的 .csproj 看起来像

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <PublishReadyToRun>true</PublishReadyToRun>
    <PublishSingleFile>true</PublishSingleFile>
    <PublishTrimmed>true</PublishTrimmed>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
  </PropertyGroup>

  <ItemGroup>
    <Content Include="*.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <ExcludeFromSingleFile>true</ExcludeFromSingleFile>
    </Content>
  </ItemGroup>
Run Code Online (Sandbox Code Playgroud)

刚刚用 .config 文件做了一些进一步的测试

<ItemGroup>
    <None Update="*.config">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <ExcludeFromSingleFile>true</ExcludeFromSingleFile>
    </None>
  </ItemGroup>
Run Code Online (Sandbox Code Playgroud)

这对我有用,上面的其他配置。