Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
Reference
MSBuild
MSBuild Concepts
MSBuild Items
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
Visual Studio
MSBuild Items

Updated: November 2007

Items represent inputs into the build system that are grouped into item collections based on their user-defined collection names. These item collections can be used as parameters for tasks, which use the individual items contained in the collection to perform the steps of the build process.

Items are declared in the project file by creating an element as a child of an ItemGroup element with the name of the item. The Include attribute of the element specifies what files to include with that item collection. For example, the following code creates an item collection named Compile, which includes two files.

<ItemGroup>
    <Compile Include = "file1.cs"/>
    <Compile Include = "file2.cs"/>
</ItemGroup>

The following code creates the same item collection by declaring both files in one Include attribute, separated by a semicolon.

<ItemGroup>
    <Compile Include = "file1.cs;file2.cs"/>
</ItemGroup>

You reference item collections throughout the project file with the syntax @(ItemCollectionName). For example, you would reference the item collection in the previous example with @(Compile). This syntax allows you to pass item collections to tasks by specifying the item collection as a parameter of that task. For more information, see How to: Use Wildcards to Build All Files in a Directory.

By default, items in item collections are separated by semicolons (;) when expanded. Use the syntax @(ItemCollectionName, 'separator') to specify a separator other than the default. For more information, see How to: Display an Item Collection Separated with Commas.

You can use the **, * and ? wildcards to specify a group of files as inputs for a build instead of listing each file separately. For example, to specify all the .cs files or .vb files in the same directory as the project file, use one of the following elements in your project file:

<CSFile Include="*.cs"/>

– or –

<VBFile Include="*.vb"/>

Note:

You can only use wildcards with items to specify the inputs for a build. For example, you cannot use wildcards to specify the inputs to the Sources parameter in the Csc task. MSBuild will accept wildcards as parameter values, but will treat them as string literals and not evaluate them. The following example would use the string literal *.cs as the Sources parameter value.

<Target Name="Compile">
    <CSC Sources="*.cs" />
</Target>

For more information on wildcards, see How to: Use Wildcards to Build All Files in a Directory.

Items can also contain the Exclude attribute, which excludes specific files from the item collection. The Exclude attribute is useful when used in conjunction with wildcards. For example, the following code adds every .cs file in the directory except the DoNotBuild.cs file.

<ItemGroup>
    <CSFile
        Include="*.cs"
        Exclude="DoNotBuild.cs"/>
</ItemGroup>

For more information, see How to: Build All Files in a Directory Except One.

Items may contain metadata in addition to the information gathered from the Include and Exclude attributes. This metadata can be used by tasks that require more information about items, or to batch tasks and targets. For more information on batching, see MSBuild Batching.

Item metadata is declared in the project file by creating an element with the name of the metadata as a child element of the item. An item can have zero or more metadata values. For example, the following element has Culture metadata with a value of Fr.

<ItemGroup>
    <CSFile Include="main.cs">
        <Culture>Fr</Culture>
    </CSFile>
</ItemGroup>

You reference item metadata throughout the project file with the syntax %(ItemMetadataName). When ambiguity exists, this can be qualified by the name of the item collection, for example %(ItemCollectionName.ItemMetaDataName).The following example uses the Display metadata to batch the Message task. For more information on using item metadata for batching, see How to: Batch Tasks By Using Item Metadata.

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <ItemGroup>
        <Stuff Include="One.cs" >
            <Display>false</Display>
        </Stuff>
        <Stuff Include="Two.cs">
            <Display>true</Display>
        </Stuff>
    </ItemGroup>
    <Target Name="Batching">
        <Message Text="@(Stuff)" Condition=" '%(Display)' == 'true' "/>
    </Target>
</Project>

Whenever an item is added to an item collection, that item is created and assigned some well-known item metadata, such as %(Filename), which contains the file name of the item. For a complete list of well-known item metadata, see MSBuild Well-known Item Metadata.

Item collections can be transformed into new item collections. For example, an item collection of .cpp files can be transformed into a collection of .obj files using an expression like @(CppFiles -> '%(Filename).obj'). For more information, see MSBuild Transforms.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
You can also Remove items from an ItemGroup      Steve St Jean   |   Edit   |  

There is an attribute of the Item element missing from this page. The Remove attribute will remove one or more items from an itemgroup. This is new to MSBuild 2008/Framework 3.5. For more information see:

Dynamically Adjust Metadata in an Item Group at http://msdn2.microsoft.com/en-us/library/bb651786.aspx

Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker