VS Code:如何根据构建配置将文件复制到输出目录?

12

我刚在VS Code中开始了一个新项目(C#,.NET Core)。 我想要像在Visual Studio中那样能够从我的项目目录复制文件到输出目录。但是,我还想根据是否为32位或64位进行复制特定的文件。

我已经找过但是目前我只学会了如何无论我的构建配置都可以复制文件。


@ZohirSalakCeNa 我没有使用Visual Studio。 - Mathew O'Dwyer
你尝试过这篇帖子吗?https://dev59.com/-VcP5IYBdhLWcg3wd5qm - Daniel
@Daniel 那是我找到的第一篇文章,但它没有告诉我如何处理不同的架构。 - Mathew O'Dwyer
尝试使用 File.Copy(sourcePath, C:\File.txt); 进行复制操作。要获取您的项目目录,请使用 Environment.CurrentDirectory - SunAwtCanvas
@Richardissimo 我已经提到了我找到的研究以及为什么对我不起作用。我说过我知道如何使用VS Code复制文件,但不知道如何根据构建配置复制文件。 - Mathew O'Dwyer
显示剩余2条评论
1个回答

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

步骤:

  1. 运行dotnet new console创建控制台应用程序。
  2. foo.txtfoo.xml添加到项目文件夹中。
  3. 按上面所述编辑.csproj文件。
  4. 使用多个配置构建项目。dotnet build -c Release -r win-x86
  5. foo.xml仅在x-64构建时复制,而foo.txt则在两个RID中都复制

输出文件夹


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接