<ItemGroup>
<Compile Remove="Class.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Class.cs" />
</ItemGroup>
For .NET Framework or other non-SDK projects, the Compile
item is constructed explicitly in the project file by listing all the source files.
For C++ projects, source files are explicitly added to the ClCompile
element in the project file.
When you hand-author an MSBuild project file without using an SDK, you can list each source file separately in the project file, or you can use wildcards to include all the files in one directory or a nested set of directories. You can also use the techniques in this article to modify the Compile
item list (in .NET projects) or ClCompile
item list in C++ projects to customize what files are built.
Items represent the inputs (such as source files) for a build. For more information on items, see Items.
To include files for a build, they must be included in an item list. As discussed previously, in .NET SDK and .NET Framework projects, the item list for the source files is Compile
. You don't see the Compile
item list in .NET SDK projects, because it's defined in the implicit imports. See Use project SDKs.
Project files that don't rely on the standard imports can use an arbitrary item list name, such as VBFile
or CSFile
. See the Example 1 and Example 2 later in this article. To set up a build based on the item list, you pass this by name to a build task, as discussed later in this article.
Multiple files can be added to item lists by either including the files individually or using wildcards to include many files at once.
To declare items individually
Use the Include
attributes similar to following:
<Compile Include="Form1.cs"/>
<Compile Include="Form1.vb"/>
If items in an item collection are not in the same directory as the project file, you must specify the full or relative path to the item. For example: Include="..\..\Form2.cs"
.
The same item list can be repeatedly modified by multiple Include
attributes. Each Include
adds to what was there previously.
To declare multiple items
Use the Include
attributes similar to following:
<Compile Include="Form1.cs;Form2.cs"/>
<Compile Include="Form1.vb;Form2.vb"/>
You can also use wildcards to recursively include all files or only specific files from subdirectories as inputs for a build. For more information about wildcards, see Items
The following examples are based on a project that contains graphics files in the following directories and subdirectories, with the project file located in the Project directory:
Project\Images\BestJpgs
Project\Images\ImgJpgs
Project\Images\ImgJpgs\Img1
To include all .jpg files in the Images directory and subdirectories
Use the following Include
attribute:
Include="Images\**\*.jpg"
To include all .jpg files starting with img
Use the following Include
attribute:
Include="Images\**\img*.jpg"
To include all files in directories with names ending in jpgs
Use one of the following Include
attributes:
Include="Images\**\*jpgs\*.*"
Include="Images\**\*jpgs\*"
Excluding and removing items
You might want to specify files that match a certain pattern, with some exceptions. You can do that in a single operation with a combination of Include
and Exclude
.
<ItemGroup>
<!-- Include every C# source file, except anything in the "sub" folder -->
<Compile Include="**/*.cs" Exclude="sub/**/*.cs">
</ItemGroup>
To remove an item that was previously included, or was included by default by an SDK, you can use the Remove
attribute.
<ItemGroup>
<!-- Remove anything in the "sub" folder -->
<Compile Remove="sub/**/*.cs">
</ItemGroup>
Pass items to a task or target
In most project files, you don't need to explicitly pass the Compile
item to a target or task, since this is handled by the standard imports. But in the case of a target the project file, you can use the @()
notation in tasks to specify an entire item list as the input for a build. You can use this notation whether you list all files separately or use wildcards.
Use the Include
attributes similar to the following:
<CSC Sources="@(CSFile)">...</CSC>
<VBC Sources="@(VBFile)">...</VBC>
You must use wildcards with items to specify the inputs for a build; you cannot specify the inputs using the Sources
attribute in MSBuild tasks such as Csc or Vbc. The following example is not valid in a project file:
<CSC Sources="*.cs">...</CSC>
Example 1
The following code example shows a project that includes all of the input files separately.
<Project DefaultTargets="Compile"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003" >
<PropertyGroup>
<Builtdir>built</Builtdir>
</PropertyGroup>
<ItemGroup>
<CSFile Include="Form1.cs"/>
<CSFile Include="AssemblyInfo.cs"/>
<Reference Include="System.dll"/>
<Reference Include="System.Data.dll"/>
<Reference Include="System.Drawing.dll"/>
<Reference Include="System.Windows.Forms.dll"/>
<Reference Include="System.XML.dll"/>
</ItemGroup>
<Target Name="PreBuild">
<Exec Command="if not exist $(builtdir) md $(builtdir)"/>
</Target>
<Target Name="Compile" DependsOnTargets="PreBuild">
<Csc Sources="@(CSFile)"
References="@(Reference)"
OutputAssembly="$(builtdir)\$(MSBuildProjectName).exe"
TargetType="exe" />
</Target>
</Project>
Example 2
The following code example uses a wildcard to include all the .cs files.
<Project DefaultTargets="Compile"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003" >
<PropertyGroup>
<builtdir>built</builtdir>
</PropertyGroup>
<ItemGroup>
<CSFile Include="*.cs"/>
<Reference Include="System.dll"/>
<Reference Include="System.Data.dll"/>
<Reference Include="System.Drawing.dll"/>
<Reference Include="System.Windows.Forms.dll"/>
<Reference Include="System.XML.dll"/>
</ItemGroup>
<Target Name="PreBuild">
<Exec Command="if not exist $(builtdir) md $(builtdir)"/>
</Target>
<Target Name="Compile" DependsOnTargets="PreBuild">
<Csc Sources="@(CSFile)"
References="@(Reference)"
OutputAssembly="$(builtdir)\$(MSBuildProjectName).exe"
TargetType="exe" />
</Target>
</Project>
Related content
How to: Exclude files from the build
Items