VS Code: How to copy files to output directory depending on build configurations

10,141
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.1</TargetFramework>
  </PropertyGroup>
  <ItemGroup Condition="'$(RuntimeIdentifier)' == 'win-x86'  Or '$(RuntimeIdentifier)' == 'win-x64'">
    <None Update="foo.txt">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
    </ItemGroup>
  <ItemGroup Condition="'$(RuntimeIdentifier)' == 'win-x64'">
    <None Update="foo.xml">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>
</Project>

Steps:

  1. Create a console app by running dotnet new console
  2. Add foo.txt and foo.xml to the project folder.
  3. Edit the .csproj file as above.
  4. Build the project with multiple configurations. dotnet build -c Release -r win-x86
  5. foo.xml is copied only for a x-64 build whereas foo.txt is copied for both RID's

Output folder

Share:
10,141
Mathew O'Dwyer
Author by

Mathew O'Dwyer

I get a little to excited about game engine development. C#, Java, I cry when I have to read example code in C/C++.

Updated on July 22, 2022

Comments

  • Mathew O'Dwyer
    Mathew O'Dwyer 4 months

    I just started a new project in VS Code (C#, .NET Core). Anyways, I want to be able to copy files from within my project directory to the output directory like I can in visual studio. But I also want to copy specific files depending on whether or not I'm building for 32 or 64 bit.

    I've looked around but so far all I've learnt how to do is copy files regardless of my build configurations.