5

I'm attempting to setup a .csproj file to have a conditional item group which will remove all of the elements in the <ProjectReference> item group.

For example:

<ItemGroup> <ProjectReference Include="..\..\..\..\Projects\Registrar\Ucsb.Sa.Registrar.Common\Ucsb.Sa.Registrar.Common\Ucsb.Sa.Registrar.Common.csproj"> <Project>{1EDDDE57-0181-41B4-B2AE-FB76450F85C8}</Project> <Name>Ucsb.Sa.Registrar.Common</Name> </ProjectReference> </ItemGroup> <ItemGroup Condition="$(OnBuildServer) == 'true'"> <ProjectReference Remove="*" /> </ItemGroup> <ItemGroup Condition="$(OnBuildServer) == 'true'"> <Reference Include="Ucsb.Sa.Registrar.Common"> <SpecificVersion>False</SpecificVersion> <HintPath>$(RegCommonDll)</HintPath> </Reference> </ItemGroup> 

But, when I load the project into VS 2008, I get the error message 'The attribute "Remove" in element <ProjectReference> is unrecognized". The odd thing is that the Remove attribute is in the schema (C:\Program Files\Microsoft Visual Studio 9.0\Xml\Schemas\1033\MSBuild\Microsoft.Build.Core.xsd). There is MSDN documentation on it (http://msdn.microsoft.com/en-us/library/bb651786.aspx). And, there is a comment about it at the bottom of the MSDN article titled "MSBuild Items".

The .csproj file seems to be pointing to .NET 3.5; but I am unable to verify if that version of msbuild is being used to load the project (does anyone know how to do that?)

First line of .csproj file:

<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 

PS. I got the idea to use the conditionals from Build with msbuild and dynamically set project references

1
  • I also tried changing the registry keys to default to 3.5. These keys don't configure Visual Studio, but instead configure the default values when creating a new project (msdn.microsoft.com/en-us/library/bb397428.aspx, \HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\3.5\, \HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions\2.0). That, of course, didn't do the trick. I also went through the PATH Environment Variable and replaced all references to the 2.0 directory with 3.5. Again, this didn't produce a solution. Commented Jul 24, 2009 at 2:07

1 Answer 1

15

Update

Remove support outside of targets was added in MSBuild 15, my answer below is now outdated.

The Remove attribute removes specific items (files) from the item type. This attribute was introduced in the .NET Framework 3.5 (inside targets only). Both inside and outside targets are supported starting in MSBuild 15.0.

Original answer

From https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-items?view=vs-2022#BKMK_RemoveAttribute

You cannot use the Remove attribute with static Items. Static items are those declared outside of targets. You can only use this attribute inside of dynamic item declarations. Dynamic item declarations are those found inside of a target. For example take a look at the following build script.

<Project ToolsVersion="3.5" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <ItemGroup> <ProjectReference Include="One.dll"/> </ItemGroup> <Target Name="Demo"> <ItemGroup> <ProjectReference Remove="@(ProjectReference)"/> </ItemGroup> <Message Text="ProjectReference : @(ProjectReference)"/> </Target> </Project> 

Also note that you should not use Remove="*" that will not remove everything. It will remove every file in the current directory which is contained in the ProjectReference item group. If you want to clear out an item you have to do Remove="@(ProjectReference)" where ProjectReference is the item.

Sign up to request clarification or add additional context in comments.

5 Comments

Is there a way to remove static items?
@citizenmatt you can do that with an ItemGroup in a target, but you cannot use Remove on an ItemGroup outside of a target.
@SayedIbrahimHashimi Is this still the case in the latest MSBuild (17)? The docs suggest that is indeed the case: learn.microsoft.com/en-us/visualstudio/msbuild/…
No wait, now I see it says Both inside and outside targets are supported starting in MSBuild 15.0
@OhadSchneider yes that is true, I've updated my answer to indicate this. Thank you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.