93

I'm frequently adding a lot of content files (mostly images and js) to my ASP.NET project. I'm using VS publish system, and on publish, new files are not published until I include them in the project. I would like to auto include all files in specified directory. Is there a way to specify which directories should be auto-included in csproj file or anywhere else?

harriyott
  • 10,138
  • 9
  • 61
  • 100
Marko
  • 5,239
  • 2
  • 25
  • 47
  • see: this may help you http://stackoverflow.com/questions/1743432/how-can-i-automatically-add-existing-items-to-a-visual-studio-project/1756240 – Saar Mar 31 '10 at 08:28
  • 1
    not exactly what I'm looking for – Marko Mar 31 '10 at 10:53
  • I updated my answer concering your issue when modifying the folder inside your vs solution browser. – Filburt Apr 07 '10 at 20:46

8 Answers8

144

Old thread, I know, but I found a way to do this that I keep forgetting, and on my search to find it one last time, I stumbled upon this question. The best way I've found to this is is to use the BeforeBuild target in the .csproj file.

<Target Name="BeforeBuild">
    <ItemGroup>
        <Content Include="**\*.less" />
    </ItemGroup>
</Target>

VS 2010 won't touch this section, and it ensures that your files are included as content every time the project is built.

Chris
  • 26,212
  • 23
  • 116
  • 214
  • What is the meaning of .less? And what means the whole string `**\*.less` ? – Registered User Jun 25 '12 at 17:03
  • 3
    .less files are css files meant to be parsed by the Less CSS preprocessor. Google "dot less" for more info on that. The expression `**\*.less` means include all *.less files in all directories. In MSBUILD speak, ** means 'all directories recursively' – Chris Jun 25 '12 at 18:08
  • Will this add the items to the Solution Explorer in Visual Studio (specifically 2012)? – jacobappleton May 21 '13 at 20:35
  • No, this does not serve to "include in project", iirc. – Chris May 21 '13 at 21:55
  • Works a storm on VSExpress 2012 for Windows Phone – funkybro Aug 27 '13 at 15:42
  • How would you include this in the Pre-build event dialog in VS? Is it verbatim? – David d C e Freitas Mar 14 '14 at 05:20
  • I don't believe the two are equivalent, David. Not sure if you can do this via pre-build event. – Chris Mar 14 '14 at 14:09
  • I have been beating my head against this problem for a while with typescript. Thanks for the solution! – Gent Sep 30 '14 at 18:57
  • 4
    At least in VS 2012, as soon as you add/remove a file from the project and save, this unfortunately gets expanded to the full list. :( – Chris Phillips Jan 14 '15 at 01:18
  • Not working for me :( When i deploy my project all files go to /bin folder, not to the root. Anyone has the same problem? – Ilia P Jan 15 '16 at 13:43
  • 3
    This worked for my situation only after changing BeforeBuild to AfterBuild, my build kicks off a powershell script that moves files around, which would then not be picked up by my azure web deploy attempt because they only existed after the build was successful. Seeing "BeforeBuild" keyed me of that there was probably an "AterBuild" as well. Hope this helps someone else. – Luke Rice Apr 02 '16 at 21:12
  • 1
    @John see [my answer below](https://stackoverflow.com/a/49049593/238722) for a fix to VS2015+. – Jesse Mar 01 '18 at 12:19
55

You simply can extend your website .csproj file. Just add your content root folder with a recursive wildcard:

...
<ItemGroup>
    <!-- your normal project content -->
    <Content Include="Default.aspx" />

    <!-- your static content you like to publish -->
    <Content Include="Images\**\*.*" />
</ItemGroup>
...

Doing so makes this folder and all content below visible inside your solution browser.

If you try to hide the folder inside the solution browser by specifying

<Content Include="Images\**.*.*">
    <Visible>false</Visible>
</Content>

it will not be published.


Update

As you already discovered the wildcard will be replaced as soon as you touch the folder inside your solution because VS projects are not designed to contain arbitrary content.

So you will have to make sure the folder and its contents are never modified from within VS - adding or removing files can only be done on the file system ... which is what you wanted as i understood your question.

It would be easier if the folder could be hidden in VS but i couldn't find a way to hide it AND publish.

Another unsuccessful approach was to include the folder by a CreateItem Task. This resulted in the contents of folder being published to \bin\app.publish\... and could not be convinced to publish it together with the content items inside the .csproj so i did not present it in my answer.

Filburt
  • 16,221
  • 12
  • 59
  • 107
  • 3
    It works until I add or remove file manually. After that line disappears from project file. – Marko Apr 07 '10 at 19:22
  • 2
    @Marko is correct. After adding `` it worked. Once you add more images the .csproj is changed and is back to listing all files in the images/ ... and the is gone. – Ravi Ram Feb 02 '12 at 03:07
  • Stick this code in a separate .proj file and call that from the before build target in the .csproj file. – NL - Apologize to Monica Oct 28 '16 at 15:43
22

For those having issues using Chris' answer, this is the solution for Visual Studio 2012 and newer:

<Target Name="ContentsBeforeBuild" AfterTargets="BeforeBuild">
  <ItemGroup>
    <Content Include="images\**" />
  </ItemGroup>
</Target>

As Chris mentioned in his answer - Visual Studio will not touch this <Target> section, even if you manually fiddle around (adding/removing files) with the target directory.

Please note that you should include a subdirectory where the files are located (in the case above, it's images). Visual Studio/MSBuild will place those files in the same directory within the project structure. If you don't use a subdirectory, the files will be placed at the root of the project structure.

For a quick explanation of the wildcards:

  • ** means everything recursively (files, subdirectories, and files within those)
  • *.ext will include all files with extension ext within the top-level directory, but not subdirectories
    • For example, *.ext could be *.png, *.js, etc. Any file extension will work
  • **\*.ext will include all files with extension ext from the top-level directory and all subdirectories.
  • See the answer from How do I use Nant/Ant naming patterns? for a more complete explanation with examples.

For completion, please note that there is a difference between using <Target> and not using it.

With the <Target> approach, Visual Studio will not show the files within the Solution Explorer.

<Target Name="ContentsBeforeBuild" AfterTargets="BeforeBuild">
  <ItemGroup>
    <Content Include="images\**" />
  </ItemGroup>
</Target>

The non-<Target> approach will instruct Visual Studio to show the files within the Solution Explorer. The drawback with this one is that any manipulation of the automatic directories will cause Visual Studio to override the wildcard entry. It should also be noted that the approach below will only update the Solution Explorer upon opening the Solution/Project in VS. Even the Solution Explorer's "refresh" toolbar button won't do it.

<ItemGroup>
  <Content Include="images\**" />
</ItemGroup>
Jesse
  • 8,035
  • 7
  • 42
  • 56
  • 1
    Thanks for experimenting and pointing out the differences between using and not. Great explanation of the details of the wildcards, too. – Kurt Hutchinson Mar 16 '18 at 20:21
  • @KurtHutchinson - no problem. =) – Jesse Mar 17 '18 at 02:00
  • I believe this solution needs polishing. When you instruct target to run after "BeforeBuild" then in theory this could also happen AFTER publish. Current solution probably works due to luck. – Imre Pühvel Aug 10 '18 at 09:05
  • @ImrePühvel how do you figure publish happens *before* the `BeforeBuild` event? MSBuild must first build the project and binaries before it can even consider publishing. – Jesse Aug 10 '18 at 12:55
  • 1
    I did not claim publish to happen before build, but that the declaration does not guarantee that the items are added to content before publish. From code sample :`.. AfterTargets="BeforeBuild"`. Meaning, yuour custom target must execute after BeforeBuild, but it does not specify how much after. Though, my mistake, by the current target ordering algorithm it should be ok: https://msdn.microsoft.com/en-us/library/ee216359.aspx – Imre Pühvel Aug 10 '18 at 13:24
  • Man did I ever want this to work. Unfortunately nothing happens when you try this in VS2017 v15.9.5 – Bitfiddler Jan 12 '19 at 00:11
  • Here is what worked for me: – Bitfiddler Jan 12 '19 at 01:11
  • I had to open up the `` tag and put in `Always` to get it to work. – Andrew Keeton Jan 18 '19 at 19:59
  • This is the correct solution even with ASP.NET 4.7, especially when working with webpack and dynamic content – Raffaeu Feb 15 '20 at 12:01
10

You can use the framework's System.IO.Directory.GetFile(string) method and its overloads to recursively include all files.

  <ItemGroup>
    <Content Include="$([System.IO.Directory]::GetFiles('$(ProjectDir)Scripts\', '*.js', SearchOption.AllDirectories))" />
    <Content Include="$([System.IO.Directory]::GetFiles('$(ProjectDir)Images\', '*.png', SearchOption.AllDirectories))" />
  </ItemGroup>
Steven Liekens
  • 10,761
  • 4
  • 50
  • 77
  • This was a big help to me; I have multiple directories a few levels deep and lots of files I wanted auto-included and this turned all of those contents into one. Thanks! – Jay Otterbein Oct 16 '15 at 22:42
  • 2
    I experimented with this some more and it turns out that this has all the same limitations as `Include="**\*.ext"` with wildcards. – Steven Liekens Oct 17 '15 at 09:20
  • Wow, do project file includes run powershell? Is that sandboxed at all? That's an exploit waiting to happen. – Brain2000 Sep 23 '19 at 18:38
  • It's not PowerShell. MSBuild has its own syntax for invoking static methods. According to docs, this is limited to a system classes. https://docs.microsoft.com/en-us/visualstudio/msbuild/property-functions?view=vs-2019#static-property-functions Although I'm sure you can just powershell with any arguments... – Steven Liekens Sep 24 '19 at 11:04
3

I've written up how I was able to get the content includes created with a small powershell script:

$folders = Get-ChildItem .\ -r -Directory
$filtered = $folders |Select-Object @{Name='FullName';Expression={$_.fullname.replace($pwd,'')}}, @{Name='FolderDepth';Expression={($_.fullname.Split('\').Count) - ($Pwd.Path.split('\').count)}} | Sort-Object -Descending FullName,folderDepth 
$basefolders = $filtered | Where-Object{$_.folderdepth -eq 1}
$basefoldersobj = @()
foreach($basefolder in $basefolders)
{
  $basefoldername =$baseFolder.fullname
  $filteredbase = $filtered -match "\$basefoldername\\" | Sort-Object -Descending FolderDepth | Select-Object -first 1
  if($filteredbase -eq $null)
  {
    $filteredbase = $filtered -match "\$basefoldername" | Sort-Object -Descending FolderDepth | Select-Object -first 1
  }
  $obj = New-Object psobject
  Add-Member -InputObject $obj -MemberType NoteProperty -Name 'Folder' -Value $basefolder.fullname.trim('\')
  Add-member -InputObject $obj -MemberType NoteProperty -Name 'DeepestPath' -Value $filteredbase.folderDepth
  $basefoldersobj += $obj
}
$include = '*.*'
foreach($bfolderObj in $basefoldersobj)
{
  $includecount = ''
  $includecount = "\$include" * ($bfolderObj.Deepestpath)
  Write-Output "<content Include=`"$($bfolderObj.folder)$includecount`" /> "
}

This should produce the necessary include statement at the powershell prompt

Nathaniel Ford
  • 16,853
  • 18
  • 74
  • 88
thom schumacher
  • 1,217
  • 8
  • 19
2

You can add files with links like this, they are searchable, view-able, but they do not checkout if you try to change them, also visual studio leaves the wildcards in place:

  <ItemGroup>
    <Content Include="..\Database Schema\Views\*.sql">
      <Link>Views\*.sql</Link>
    </Content>
  </ItemGroup>

This goes inside the .proj file.

chongo2002
  • 121
  • 1
  • 6
  • 12
1

Not to my knowledge; however my suggestion is to paste them into the project as this will include them by default. So, instead of pasting them into the directory via Explorer, use Visual Studio to paste the files into the folders.

kamranicus
  • 3,857
  • 2
  • 34
  • 51
-2

I realized that best solution for this is manually add files, one by one. If you have hundreds of them as I did it was just a matter of few hours. Funny that even in 2016 with VS 2015 this serious problem is still not solved. Ahh, how I love Xcode.