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?
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With