简体   繁体   中英

VS2019 .csproj Delete or Modify Linked Content Files

I have an external project with a multitude of "common" javascript/etc content files to be shared across multiple web projects. Using the new LinkBase property in my csproj file, along with a combination of the trick here: http://mattperdeck.com/post/Copying-linked-content-files-at-each-build-using-MSBuild.aspx I now have the exact hierarchy of the linked project copied to the main project, and when I run my website the content files function and are found, and the bundling is even happening correct, which is great!

Issue is, after one closes the application, the copied content files remain behind, and since its a team project, it could become confusing for other devs which files are the "master" copies and which are the copied, there's no visual indicator for this.

Is there a way I can do one of the two following?

A: During the copy task, prepend all files with some commented text indicating something like "This was copied from the Linked Project, dont edit this file, all changes will be lost, go edit the file over here ->"

Or:

B: Is there a way I can delete the files once the project is closed? I'm not seeing any build events that happen after the project completes so I am guessing this option is likely not possible.

Here's what I currently have, which is pretty straightforward:

<ItemGroup>
  <Content Include="..\MyFileLinks\**\*.js" LinkBase="" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

<Target Name="CopyLinkedContentFiles" BeforeTargets="Build">
  <Copy SourceFiles="%(Content.Identity)"
        DestinationFiles="%(Content.Link)"
        SkipUnchangedFiles='true'
        OverwriteReadOnlyFiles='true'
        Condition="'%(Content.Link)' != ''" />
</Target>

The best way is to add a delete target that depends on Clean process. And actually, there is no target that relys on close vs because any targets depend on the build process rather than close VS or open VS.

So my suggestion is to add a target that relys on clean process to delete those files.

Add this target:

<Target Name="RemoveFiles" AfterTargets="Clean">
        <Message Importance="high" Text="%(Content.Link)"></Message>
        <Delete Files="%(Content.Link)"></Delete>
</Target>

Before you want to close VS, please click Clean , the files will be removed at that time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM