发布单个 .exe 时如何发布单独的 appsettings.json 文件?

use*_*960 2 c# console-application .net-core

我有一个带有配置文件的控制台应用程序。当我使用.proj 中的新PublishSingleFile设置进行发布时,它会将应用程序发布为一个文件(很好),但它还会将应用程序设置添加到该 .exe 中。

这意味着此时它实际上并不是一个配置文件,而是一个硬编码值的文件。

如何使用单独的配置文件进行发布?

当前 .proj 设置

<PropertyGroup>
   <OutputType>Exe</OutputType>
   <TargetFramework>netcoreapp3.0</TargetFramework>
   <PublishReadyToRun>true</PublishReadyToRun>
   <PublishSingleFile>true</PublishSingleFile>
   <PublishTrimmed>true</PublishTrimmed>
   <RuntimeIdentifier>win-x64</RuntimeIdentifier>
 </PropertyGroup>
Run Code Online (Sandbox Code Playgroud)

use*_*960 5

解决了它,需要在 .csproj 中添加以下内容

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