47

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?

Daniel A. White
  • 174,715
  • 42
  • 343
  • 413
Gigi
  • 24,295
  • 20
  • 85
  • 170

3 Answers3

76

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.

Sensei_Shoh
  • 519
  • 5
  • 12
Martin Ullrich
  • 78,211
  • 20
  • 211
  • 189
  • is there any problem using below as default and use include information for every included file. any performance impact? **;$(DefaultItemExcludes) – Emil Dec 28 '17 at 17:36
  • 7
    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. – Martin Ullrich Jan 08 '18 at 16:52
  • I had to use `` to get it to build when we had one project in the subfolder of another. – Segfault Mar 13 '19 at 13:53
15

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.

Xpleria
  • 4,162
  • 5
  • 43
  • 54
  • 1
    As stated by @MartinUllrich, it's better to use (_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`). – Sensei_Shoh May 30 '19 at 02:19
  • The config I posted does the same thing. `` **prevents the compiler from even looking in those directories** – Xpleria May 30 '19 at 07:08
  • 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 ``, IMO, since you just have to specify the exclude glob once instead of for each item type like you have shown. – Sensei_Shoh May 31 '19 at 11:24
  • 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`. – Sensei_Shoh May 31 '19 at 11:42
11

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.

Lex Li
  • 52,595
  • 8
  • 102
  • 129