How to not copy app.config file to output directory

11,805

Solution 1

The handling of app.config is special, it is treated By Name, the build process will select the app.config file following this order:

  • Choose the value $(AppConfig) set in the main project.
  • Choose @(None) App.Config in the same folder as the project.
  • Choose @(Content) App.Config in the same folder as the project.
  • Choose @(None) App.Config in any subfolder in the project.
  • Choose @(Content) App.Config in any subfolder in the project.

$(AppConfig) is an MSBuild Property, if it is empty, it will look for a file with name "App.Config" in the "None" or "Content" MSBuild Item Groups, if there's a match, the file will be used and will be copied to the output directory replacing the app.config name by [AssemblyName].config

If you want to keep the file without delete it, you will need to change the "Build Action" property to something different to "None" or "Content", you can use any existing value on the list (I suggest "AdditionalFiles") or any value you want to use i.e. "MyConfigFile", and now the that will keep the file inside the project, but without the logic that generates the configuration file in the output directory.

Or you can rename the file to something different than "app.config" and keep the current property values for "Build Action" and "Copy to Output Directory".

Solution 2

After visual studio 2017 app.config is copied by default to output folder. According to MS it is by design vs developercommunity

In the MSBuild there is default property invoked called AppConfig which is equal to "app.config" However even if to set that property to empty - it will still find app config using Find task and copy: https://github.com/dotnet/msbuild/blob/13522d2466ae1634177e2a6a40fefaedff95139c/src/Tasks/Microsoft.Common.CurrentVersion.targets#L1159

The best workaround I found is to remove it manually and add back as resource:

<ItemGroup>
  <None Remove="app.config" />
  <Resource Include="app.config" CopyToOutputDirectory="Never" />
</ItemGroup>

Note that after this change app.config will not be copied at all to the output directory but will be available in Solution Explorer.

Solution 3

Add /p:AllowedReferenceRelatedFileExtensions=.pdb in the MSBuild parameters to suppress library project .config files, library xml docs, etc. With the above, only PDBs are retained.

This parameter can also be added to csproj files, but personally I find it easier to manage as part of the build configuration.

Share:
11,805
David
Author by

David

Biomedical engineer and neuroscientist

Updated on June 14, 2022

Comments

  • David
    David almost 2 years

    I have a WPF application I am building. I am using Visual Studio Community 2015. In an effort to create a "true" release build, I am changing up some build settings so it only generates necessary files that will be installed on the user's computer. So no vshost exe, no generated XML files, etc. etc. etc. (I am retaining PDB file generation, however).

    I have everything exactly how I want it, except for one file. I have an App.config file in each project. I don't mind this getting copied to the output directory on my debug builds, but on my "true" release build I don't want it there.

    The file is already set as "Do not copy" in its properties. See images below:

    App.config image

    Despite having the "Do not copy" setting, however, I still get an "MyAppName.exe.config" file generated every single time I build the project. Why is this so? How can I turn it off? It is quite annoying.

    Thanks for any advice!

  • Nicolas
    Nicolas almost 7 years
    Brilliant answer, thanks for explaining these details. Funny fact: my VS2015 added an "App1.config" to my project (and I wasn't aware of the name addiction) -> result: no AssemblyName.config file was created. Renaming the App1.config to App.config immediately helped.
  • DanielV
    DanielV over 4 years
    Helped me answer this