Build All Files in a Directory

When you build a project that contains several files of the same type, each file can be listed separately in the project file, or you can use wildcards to include all the files in one directory or a nested set of directories.

In this example, the "MyApp" and "MyAppWildcard" projects contain two types of files as inputs. One version of the project file contains all the files listed separately; the other version uses wildcards for the .cs files. The two versions of the project build the same.

Specifying the Inputs for a Build

Items are the inputs for a build. Each Item element must have a Name, which is used to group items, for example <CSFile .../> identifies Visual C# files and <VBFile ... /> identifies Visual Basic files. The individual items are declared using the Include attribute, for example:

<CSFile Include="form1.cs"/>
– or –
<VBFile Include="form1.vb"/>

If an item is not in the same directory as the project file, you must specify the full path to the file or the relative path, for example, ..\..\form2.cs.

In the project file, you can use the @ notation in tasks to specify an array of all the files of one type as inputs for a build. This notation can be used whether you list all files separately or use wildcards. For example, to use all Visual C# files or all Visual Basic files as inputs:

<Csc Sources="@(CSFile)"/>
- or -
<Vbc Sources="@(VBFile)"/>

Using Wildcards to Specify the Inputs for a Build

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

<CSFile Include="*.cs"/>
– or –
<VBFile Include="*.vb"/>

If you want to use wildcards, you must use the Item element 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>

Also, you can use wildcards to recursively include all files or only specific files from subdirectories as inputs for a build. For example, for a project that contains graphics files in the following directories and subdirectories:
* "Project" directory
* "Images" directory
* "BestJpgs" directory (contains only .jpg files, and all file names start with "best")
* "Icons" directory (contains only .ico files)
* "ImgJpgs" directory (contains only .jpg files, and all file names start with "img0")
* "Img1" directory (contains only .jpg files, and all file names start with "img1")

To include all .jpg files in the Images directory and subdirectories:
Include="Images\*\.jpg"

To include all .jpg files with names starting with img:
Include="Images\*\img.jpg"

To include all files in directories with names ending in jpgs:
Include="Images\*\jpgs\."
– or –
Include="Images\*\jpgs\*"

ItemGroup Element

In the project file, Items must be grouped by an ItemGroup element. When items are grouped in an item group, you can apply conditions to all the items.

Building the Project

To build the project that contains all the files listed separately, navigate to the directory that contains the project file MyApp.proj and type:
msbuild MyApp.proj

To build the project that contains wildcards, navigate to the directory that contains the project file MyAppWildcard.proj and type:
msbuild MyAppWildcard.proj

Project Files

Download Code - C#

Visual C# example


MyApp.Proj
		    <Project MSBuildVersion="2.0" 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>
	

MyAppWildcard.Proj
		    <Project MSBuildVersion="2.0" 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>
	
Microsoft Communities