Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude App.config from getting bundled in the .exe in a .net core 3 single-file self-contained WinForms app?

I have a Winforms .NET Core 3 app that I want to publish as a Self-Contained Single-File Deployment

Here is the relevant .csproj file

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

I am using <RuntimeIdentifier>win-x64</RuntimeIdentifier> so it generates a Self Contained Deployment for Windows x64 and <PublishSingleFile>true</PublishSingleFile> so everything gets embedded in the .exe file.

When publishing by running:

dotnet publish -c Release

I get the .exe and the .pdb files at bin\Release\netcoreapp3.0\win-x64\publish

- MyApp.exe
- MyApp.pdb

What do I need to change in the .csproj file so I get the MyApp.dll.config or MyApp.exe.config whichever is correct next to the .exe so the app actually reads config from it instead of its embedded App.Config?

I have tried adding this

<ItemGroup>
  <Content Update="*.config">
    <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
    <ExcludeFromSingleFile>true</ExcludeFromSingleFile>
  </Content>
</ItemGroup>

as hinted by this link Single-file Publish - Build System Interface but it still produces only the two files.

like image 947
Carlos Muñoz Avatar asked Oct 06 '19 23:10

Carlos Muñoz


1 Answers

Your question helped me figure out this for me, so thanks. hopefully this works for you too.

my .csproj looks like

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

just done some further testing with a .config file

<ItemGroup>
    <None Update="*.config">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <ExcludeFromSingleFile>true</ExcludeFromSingleFile>
    </None>
  </ItemGroup>

that worked for me, with the other config from above.

like image 167
user1947960 Avatar answered Nov 06 '22 14:11

user1947960