简体   繁体   中英

Exclude folder from all projects using Directory.Build.props?

I have a solution of projects, each of which contains of folder "Previews". I want this folder to always be excluded from my projects.

When I "Exclude From Project" through Visual Studio, the following is added to the .csproj file:

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

If I remove that from the .csproj files, and add it to the Directory.Build.props file, it no longer excludes the file from VS.

I also tried using "$(ProjectDir)\Previews\**" and the complete path with no properties, neither of which worked.

How can this be achieved? It seems like it should be possible since VS can pick up on references declared, and many other project properties in the solution .props file.

Instead , you should use Directory.Build.targets .

Your operation is to overwrite the some items on MSBuild and that is the .targets file's function. It is imported at the bottom of the csproj file so that it can overwrite the items.(msbuild reads from the top to the bottom and take the last XML node as the final value).

But .props file is imported at the top of the csproj file and it is used as to define some new global properties. Since it imported at the top, it cannot be used as overwriting value operation and it is always overridden by properties under the main body of the csproj file.

Besides , you can this official document to get the usage of the two files.

Since your item remove is overwriting operations, you should change the file into Directory.Build.targets .

Note : the two files are embedded into the main csproj file during build. So all the msbuild properties, items,.... can be used under the two files.

Using @Mr. Qian 's answer, I was able to use Directory.Build.targets when Directory.Build.props didn't work.

For anyone looking to 'hide' specific files/globs in Visual Studio across a solution of projects, here is my Directory.Build.targets which excludes npm package.json , package-lock.json , and NuGet packages.lock.json files across all projects:

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <ItemGroup>
        <Content Remove="*lock.json" />
        <Content Remove="package.json" />
        <None Remove="*lock.json" />
        <None Remove="package.json" />
    </ItemGroup>
</Project>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM