79

I have a project in Visual Studio. I need to deploy some 3rd party files along with my code. Typically I would put this files in a "Resources" directory and set the Build Action on each file to "Content" and the Copy To Output Directory to "Copy if newer".

Is there anyway I can set these directives at the folder level. The current project I am working with has dozens of such files and a couple of sub folders. I'd like to be able to make the entire directory as "Content" and "Copy if newer".

brendan
  • 29,308
  • 20
  • 68
  • 109

6 Answers6

93

Create the project. Add one file as Content. Unload the project and edit the *proj file manually.

 <ItemGroup>
    <Content Include="myfolder**\*.dll**">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
  </ItemGroup>

And then in the content-ItemGroup I would replace that singe file with some MsBuild wildcard expression, *.dll, or whatever.

Arve
  • 7,284
  • 5
  • 37
  • 41
  • 1
    I tried this. It does work but when I added an item using Visual Studio, it removed the wildcard and changed all the individual items back to . I also tried the suggestion at http://stackoverflow.com/questions/3320190/visual-studio-how-to-override-the-default-build-action-for-certain-extension-t. but that didn't work at all. – Colin May 17 '11 at 10:29
  • 19
    This wildcard statements work for me to copy all files and sub directories: PreserveNewest – Minh Nguyen Nov 11 '16 at 13:27
  • 1
    @Arve this is gem bro, was struggling but then found your answer after 24 hours – WitVault Dec 18 '20 at 08:37
  • This works perfectly. Is there a way to also exclude the parent folder? If I have a file inside MyFolder\file.txt, how can I only copy the file and not its parent folder too? – AzeExMachina Sep 21 '21 at 07:59
11

I use Visual Studio 2012 and you can shift-click to select multiple items in the Solution Explorer then edit each item's Copy To Output Directory property all at once in the Properties window.

Granted this isn't equivalent to the solution you are looking for functionally, but semantically it is. And hopefully the next person to stumble across this post with a humongous folder to remedy (as is with me) won't have to dive into the .csproj file.

Hope this helps!

Freestyle076
  • 1,548
  • 19
  • 36
8

If you happen to have the need to set the Build Action for an entire folder the best option is to just open the .csproj file and use a regex to replace all the occurences from

<Content ....

to

<None ...

That worked just perfectly for me.

Juri
  • 32,424
  • 20
  • 102
  • 136
  • This is indeed a better solution – Radu Simionescu Nov 21 '13 at 13:22
  • Hmmm but wouldn't it be more logical if you could just set this using tools > options somewhere? It's pretty annoying that every new asset you add to a project gets excluded by default. (why would I add an asset if I didn't also want to include it in my final build?) – Kokodoko Mar 18 '15 at 09:21
  • My use case was that I had to exclude a giant (100MB) /public/ directory in the root of my project from publish (because I copied it once and it's not going to change). However I did want to include it in my project and thus in source control. This solution worked really well for that. I didn't use a regex but just used replace-all. – Gerben Rampaart Jun 12 '15 at 12:48
5

I just added this to my *.csproj file (right click Edit Project File)

<ItemGroup>
    <Content Include="MYCUSTOMFOLDER\**">
        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
</ItemGroup>

I know this answer is similar to @Arve but I don't know why this extra complexity with the .dll wildcard filter.

CodingYourLife
  • 7,172
  • 5
  • 55
  • 69
0

Edit your *.csproj or .vbproj file

Add this tag

  <ItemGroup>
    <Folder Include="YOUR_FOLDER_NAME_HERE/">
  </ItemGroup

the final file must look like this:

<Project>
<---some more tags--->
      <ItemGroup>
        <Folder Include="YOUR_FOLDER_NAME_HERE\" />
      </ItemGroup
<---some more tags--->
</Project>
jcmordan
  • 1,038
  • 13
  • 20
  • Tried this and it didn't work. I want to add an empty folder to the project when it's deployed. This didn't do it. – sr28 Aug 22 '14 at 13:05
0

If you want to preserve the recursive folder structure it is possible to add this piece of XML:

  <ItemGroup Label="bg_screens">
      <_CustomResource Include="..\..\resourcedir\**\*">
          <Link>resourcedir\%(RecursiveDir)%(FileName)%(Extension)</Link>
          <DeploymentContent>true</DeploymentContent>
      </_CustomResource>
  </ItemGroup>

The the resources will keep the tree structure instead of being added in a flat manner.

Mo0gles
  • 10,517
  • 2
  • 21
  • 15