将Visual Studio中的链接文件复制到指定位置

3

我在我的Visual Studio解决方案中设置了一个项目,其中包含许多链接文件。这些是内容文件。

当我构建项目时,我想将链接文件复制到另一个位置。这是否可能,如何实现?


这些“链接文件”需要复制到与网站完全分离的位置吗?(我假设从 asp.net 的问题标签来看,你正在构建一个网站)。 - undefined
是的,正确的。它们需要被复制到同一文件系统的不同位置。 - undefined
5个回答

16

太准确了!谢谢! - undefined

2

我不得不依赖另一种技术。多年来,我一直在使用@user1147862的解决方案,但是在Visual Studio 2022/.NET 6中,它根本不起作用。Visual Studio似乎不再“看到”任何添加为链接的内容文件,并且复制操作基本上什么也没做。

所以现在我做的是,在我的情况下,我有一堆JS文件,我将它们包含到wwwroot/common/js下的多个项目中。我在CSPROJ文件中添加以下内容...

<ItemGroup>
  <!-- Adds the files as links in the Solution Explorer.
       When you double-click, it opens the original file. -->
  <Content Include="..\..\Common\**\*.js">
    <Link>wwwroot\Common\%(RecursiveDir)%(FileName)%(Extension)</Link>
  </Content>

   <!-- Picked up by the Copy below.  Using "Content"
        with "Link" adds external files as links.  But a custom
        name is needed for use with Copy. -->
  <CommonJs Include="..\..\Common\**\*.js" />

  <!-- Tells Visual Studio to ignore any actual files at this location.
       I found that if I didn't do this, Visual Studio sees the copied
       file instead of the linked file.  This tells Visual Studio to
       ignore the copied file so that when I double-click it in
       Solution Explorer, it opens the original copy. -->
  <Content Remove="wwwroot\Common\**" />
</ItemGroup>

<Target Name="CopyCommonJs" BeforeTargets="Build">
  <Copy SourceFiles="@(CommonJs)"
        DestinationFiles="wwwroot\Common\%(RecursiveDir)%(Filename)%(Extension)"
        SkipUnchangedFiles="true"
        OverwriteReadOnlyFiles="true" />
</Target>

整个方法都是可行的,我现在理解了所有的部分,但我更喜欢旧的方式并希望它仍然能够工作。如果有人找到更优雅的解决方案,请告诉我。


1

如果您右键单击项目,然后选择“生成事件”选项卡,您应该能够创建一个后期生成事件脚本,该脚本将获取您指定的任何文件并将其复制到您指定的位置。

一个好的起点是这里


谢谢你的回复,但是由于这些文件是链接文件,它们并不在项目的文件系统中存在。我该如何处理这个问题? - undefined
你仍然可以使用XCOPY来定位它们 - 基本上,后构建命令存储在一个.bat文件中,环境在项目构建完成后执行该文件。如果文件本身不会被移动,并且你可以依赖于它们的绝对路径,在构建事件脚本中使用绝对路径并将它们移出去。 - undefined

0

看起来只需简单地添加

<CopyToOutputDirectory>Always</CopyToOutputDirectory>

将Visual Studio 2017 csproj项目文件中的链接文件项设置为"复制到生成目录",无论您将构建操作声明为None还是Content,都会在构建时将链接文件复制到输出目录。
例如,以下两种方式都有效...
<None Include="..\configuration\Shared.OctopusDeploy.config">
  <Link>Shared.OctopusDeploy.config</Link>
  <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>

<Content Include="..\configuration\Shared.OctopusDeploy.config">
  <Link>Shared.OctopusDeploy.config</Link>
  <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>

很遗憾,我认为这在VS2022 / SDK格式项目中不再适用了。 - undefined

0
你可以使用MSBuild来完成这个任务。具体来说,可以使用它的Copy Files任务:

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