11

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.

Mathew O'Dwyer
  • 187
  • 1
  • 1
  • 7
  • Right Click on your Solution ==> Properties ==> Build ==> Platform Target, That how you'd know – Rainbow Apr 29 '18 at 05:33
  • @ZohirSalakCeNa I'm not using visual studio. – Mathew O'Dwyer Apr 29 '18 at 05:36
  • Did you give this post a try? https://stackoverflow.com/questions/44374074/copy-files-to-output-directory-using-csproj-dotnetcore – Daniel Apr 29 '18 at 06:03
  • @Daniel That was the first post I found, however it doesn't tell me how to handle different architectures. – Mathew O'Dwyer Apr 29 '18 at 06:12
  • Try `File.Copy(sourcePath, C:\File.txt);` And to get your Project directory, use `Environment.CurrentDirectory` – SunAwtCanvas Apr 29 '18 at 06:18
  • Thanks for posting your first question. For future reference, it's worth reading [How to ask](https://stackoverflow.com/help/how-to-ask), which advises to post in your question what your research is and why it did not work for you, since it wastes the time of the people volunteering their time to answer your question. – Richardissimo Apr 29 '18 at 07:25
  • @Richardissimo I've mentioned what research I've found and why it doesn't work for me already. I said that I know how to copy files using VS Code, but not how to copy files depending on the build configurations. – Mathew O'Dwyer Apr 29 '18 at 08:52

1 Answers1

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

GuruCharan94
  • 852
  • 9
  • 13