How do I exclude files/folders from a .NET Core/Standard project?

.NetMsbuild.Net CoreVisual Studio-2017.Net Standard

.Net Problem Overview


In .NET Core and .NET Standard projects, if you put files and folders within the project directory, they are automatically picked up by Visual Studio; essentially they are part of the project.

What if I have files/folders in there that aren't really part of the project itself (in terms of code or content) - short of removing them altogether, is there a way I can exclude them from the project as I can with projects targeting the full .NET Framework?

.Net Solutions


Solution 1 - .Net

There are also a few things you can do in the csproj files to make sure the files aren't picked up:

  1. Make sure none of the globbing patterns that look for "project items" pick up the files:

    $(DefaultItemExcludes);your_nonproj.file;a***.pattern

  2. Remove items explicitly:

Note that, on large directories (number of files), using DefaultItemExcludes with the folder\** pattern is a lot faster since msbuild will skip walking the directory entirely. Using a remove for this will still let msbuild spend quite some time discovering files.

Solution 2 - .Net

Just to be complete, if you're using ItemGroup to exclude folder, then:

<ItemGroup>
  <Content Remove="excluded_folder\**" />
  <Compile Remove="excluded_folder\**" />
  <EmbeddedResource Remove="excluded_folder\**" />
  <None Remove="excluded_folder\**" />
</ItemGroup>

Because, I had an angular project with the node_modules folder which had very long paths and VS kept throwing exceptions. And using <Content Remove="node_modules\**\*" /> didn't work.

Solution 3 - .Net

Open the project in Visual Studio, and right click the files and folders in Solution Explorer. Choose Exclude from Project.

That's exactly what you do for projects targeting .NET Framework.

Solution 4 - .Net

In case you want to exclude files from the compilation process but still have them in the solution explorer tree, then

<ItemGroup>
	<Compile Remove="Templates\**" />
	<Content Include="Templates\**" />
</ItemGroup>

Templates are the folder name (in this case) and everything inside it will be ignored

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGigiView Question on Stackoverflow
Solution 1 - .NetMartin UllrichView Answer on Stackoverflow
Solution 2 - .NetXpleriaView Answer on Stackoverflow
Solution 3 - .NetLex LiView Answer on Stackoverflow
Solution 4 - .NetHakan FıstıkView Answer on Stackoverflow