Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
563 views
in Technique[技术] by (71.8m points)

xml - 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.

question from:https://stackoverflow.com/questions/65851625/exclude-folder-from-all-projects-using-directory-build-props

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...