73

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?

1
  • You can set the CopyToOutputFolder property for each file to Never
    – VMAtm
    Apr 2, 2017 at 23:51

4 Answers 4

111

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:

<PropertyGroup>
  <DefaultItemExcludes>$(DefaultItemExcludes);your_nonproj.file;a\**\*.pattern</DefaultItemExcludes>
</PropertyGroup>

2) Remove items explicitly:

<ItemGroup>
  <None Remove="hidden.file" />
  <Content Remove="wwwroot\lib\**\*" />
</ItemGroup>

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.

3
  • is there any problem using below as default and use include information for every included file. any performance impact? <DefaultItemExcludes>**;$(DefaultItemExcludes)</DefaultItemExcludes>
    – Emil
    Dec 28, 2017 at 17:36
  • 9
    On large directories (number of files), using DefaultItemExcludes with a 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. Jan 8, 2018 at 16:52
  • 1
    I had to use <Compile Remove="otherproject\**\*" /> to get it to build when we had one project in the subfolder of another.
    – Segfault
    Mar 13, 2019 at 13:53
33

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.

5
  • 1
    As stated by @MartinUllrich, it's better to use <DefaultItemExcludes> (especially for node_modules) because it will prevent the compiler from even looking in those directories, which will significantly reduce build times (in the case of node_modules). May 30, 2019 at 2:19
  • The config I posted does the same thing. <Compile Remove="excluded_folder\**" /> prevents the compiler from even looking in those directories
    – Xpleria
    May 30, 2019 at 7:08
  • 1
    Actually, I believe you're mistaken about that and it's not the same thing. As, again stated by @MartinUllrich, "using a remove for this will still let msbuild spend quite some time discovering files". Using Remove like you have here might prevent VS from throwing errors at you, but it's still affecting your build times. Also, you left out Content, so if there are "content" files in that excluded_folder, they would still be included. Another win for <DefaultItemExcludes>, IMO, since you just have to specify the exclude glob once instead of for each item type like you have shown. May 31, 2019 at 11:24
  • 1
    Also, I want to be clear that I'm not saying you should never use Remove. Obviously, they are both useful. All I'm saying is that, in the case of excluding files/folder that never have any business being in a build (such as node_modules), Remove is not the right tool for the job and it's better to use DefaultItemExcludes. May 31, 2019 at 11:42
  • 2
    @Sensei_Shoh The reason I like this answer is that I can't seem to get my dotnet5 database <Project Sdk="MSBuild.Sdk.SqlProj/1.16.2"> to honor <DefaultItemExcludes> at all, so I have to do it this way.
    – CrazyPyro
    Aug 31, 2021 at 22:02
14

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.

3
  • 12
    And for us that use Visual Studio Code, targeting .NET Core/.NET Standard, as asked?
    – henry700
    Jan 14, 2019 at 0:53
  • 3
    Edit the project file and add this setting to the <PropertyGroup> section. <DefaultItemExcludes>[Folder Path]/**;$(DefaultItemExcludes)</DefaultItemExcludes> Nov 19, 2019 at 14:01
  • Try to do that with folder that contains 17k+ subdirectories of user uploads, it will never complete Feb 23 at 6:12
5

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.