Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

app.config file in the new .csproj format

Visual studio have provided a slimmed-down .csproj file format.

I am trying to build a .NET Framework app (version 4.6.1), but use the new hand-editable file.

Similar to the previous behavior, I want the app.config file copied to the output directory, but renamed to <output_exe>.config (where output_exe is the name of the executable file).

What do we put in the .csproj file for this to happen?

This does not work, because it doesn't rename the file:

<ItemGroup>
  <Content Include="App.config">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </Content>
</ItemGroup>
like image 547
Andrew Shepherd Avatar asked Jan 29 '20 01:01

Andrew Shepherd


1 Answers

Just add the AppConfig property that msbuild and tooling expects for this feature:

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net472</TargetFramework>
    <AppConfig>App.config</AppConfig>
  </PropertyGroup>

</Project>

This enables the PrepareForBuild msbuild target to automatically pick up the file and subsequent build steps can also edit this file as the logical file - e.g. the SDK will modify the startup/supportedRuntime section based on the TargetFramework that is defined. Adding this as a custom item or build step would loose this ability.

like image 158
Martin Ullrich Avatar answered Oct 11 '22 05:10

Martin Ullrich