'visual studio 2019 cant exclude folder from build in .csproj

I am trying to exclude an entire folder from being build in my vscode project, I have edited my project file to look like this:

<Project Sdk="Microsoft.NET.Sdk">



  <ItemGroup>
    <Folder Include="NxTestware\........." />
    <Folder Include="NxTestware\.........." />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="System.Resources.Extensions" Version="6.0.0" />
  </ItemGroup>

    <PropertyGroup>
        <TargetFramework>netcoreapp3.1</TargetFramework>
        <DefaultItemExcludes>NxTestware\**</DefaultItemExcludes>
    </PropertyGroup>
    
</Project>


In hopes that when I build/pack the project, it excludes my folder 'NxTestware' enter image description here

I followed these instructions originally to use<Compile and Exclude=' but that didn't work https://docs.microsoft.com/en-us/visualstudio/msbuild/how-to-exclude-files-from-the-build?view=vs-2022

So I instead went with <DefaultItemExcludes>NxTestware\**</DefaultItemExcludes>, but when I build/pack my project it still trys to build files in the NxTestware folder which causes errors. Is it possible to tell my csproj file to exclude an entire folder?



Solution 1:[1]

First, undo the changes you've made.

Add the tag below to your .csproj project file to exclude those files from the compiler.

<ItemGroup>
    <Compile Remove="NxTestware\*.*" />
</ItemGroup>

You might want to add the following one to keep those files part of your project.

<ItemGroup>
    <None Include="NxTestware\*.*" />
</ItemGroup>

You can add them together to a single <ItemGroup>.

<ItemGroup>        
    <Compile Remove="NxTestware\*.*" />
    <None Include="NxTestware\*.*" />
</ItemGroup>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1