amateur
amateur

Reputation: 44605

copy linked files in visual studio to a location

I have a project set up in my visual studio solution that contains a number of linked files. These are content files.

When I build the project I would like to copy the linked files to another location. Is this possible and how can I achieve it?

Upvotes: 4

Views: 6527

Answers (5)

Aron Boyette
Aron Boyette

Reputation: 957

I have had to rely on a different technique. For years, I have been using @user1147862's solution for years, but with Visual Studio 2022 / .NET 6, it doesn't work at all. Visual Studio no longer appears to "see" any content files added as links and the Copy essentially does nothing.

So here is what I do now. In my case, I have a bunch of JS files that I include into multiple projects under wwwroot/common/js. I add the following to my CSPROJ file...

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

The whole approach works and I understand all the parts now, but I prefer the old way and wish it still worked. If someone finds something more elegant, please let me know.

Upvotes: 3

Mark Derman
Mark Derman

Reputation: 11

It seems that simply adding

<CopyToOutputDirectory>Always</CopyToOutputDirectory>

to the linked file item in a Visual Studio 2017 csproj project file will cause the linked file to be copied to the output directory on build, whether you have declared the build action as None or Content.

For example these both work...

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

Upvotes: 0

user1147862
user1147862

Reputation: 4226

For anybody stumbling on this page like myself, further to 5arx' post, I implemented an MSBuild target that copies all linked content files to their "virtual" locations (the directory where you placed the link in Visual Studio): http://mattperdeck.com/post/Copying-linked-content-files-at-each-build-using-MSBuild.aspx

Upvotes: 16

inksmithy
inksmithy

Reputation: 546

if you right click on the project, then choose the "Build Events" tab, you should be able to create a post build event script which will take any file you specify and copy it to the location you specify.

Good place to start would be here

Upvotes: 1

immutabl
immutabl

Reputation: 6903

You can do this with MSBuild. Specifically with its Copy Files task:

Upvotes: 0

Related Questions