Clean my Build

When you clean a build, all intermediate and output files are deleted, leaving only the project and component files. From the project and component files, new instances of the intermediate and output files can then be built. The library of common tasks that is provided with MSBuild includes an Exec task that you can use to run system commands.
In this example, the "Hello World" project contains a new target – Clean – that uses the Exec task to delete a directory and all files and directories that it contains. Also in this example, the Compile task is modified to create a separate directory for the output items that are deleted when the build is cleaned.

Create a Directory for the Output Items

Currently, the .exe file that is created when you compile the Hello World project is placed in the same directory as the project and source files. Typically, however, output items are created in a separate directory. In your project file, you can use the MakeDir task to create a directory for the .exe file.

*Use the Property element to define the location and name of the directory, for example, create a directory named BuiltApp in the directory that contains the project and source files.

<builtdir>BuiltApp</builtdir>

*Use the MakeDir task to create the directory if the directory does not exist. This task uses the Directories attribute to specify which directory to create, and the Condition attribute to determine whether or not to run the task. If the directory does not exist (!Exists), the task runs:

<MakeDir Directories = "$(builtdir)" Condition = "!Exists('$(builtdir)')" />

RemoveDir Task

This task runs the rd system command. This task can be used to delete a directory and all files and directories that it contains from a disk. In this example, the output directory BuiltApp and the .exe file that it contains are deleted:

<RemoveDir Directories="$(builtdir)" />

Specifying Which Targets to Use

This Hello World project file contains two targets: Compile and Clean. Compile is defined as the default target ( <Project *DefaultTargets* = "Compile"> ) and is therefore used automatically unless you specify a different target or targets. You use the command line switch /target to specify a different target, for example:

msbuild <file name>.proj /target:Clean

The /target switch can be shortened to /t and can specify more than one target, for example, to use the target Clean then the target Compile, type:

msbuild <file name>.proj /t:Clean;Compile

You can use either a comma or a semicolon to separate a list of targets. Alternatively, you can repeat the switch, for example:

msbuild <file name>.proj /t:Clean /t:Compile

Building the Project

To build one of these projects from the command line, navigate to the directory that contains the project file and type:

msbuild <file name>.proj

If the directory contains only one file and that file's extension ends in "proj", you can type only msbuild because the project file is used automatically by the .NET Build Engine.

Cleaning the Build

To delete the output directory BuiltApp and the .exe file that it contains, navigate to the directory that contains the project file and type:

msbuild <file name>.proj /t:Clean

Project Files

Download Code - C#
Download Code - VB

Visual C# example

		    <Project [DefaultTargets] = "Compile" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" >
	

		        [<PropertyGroup>]
		            <!-- Set the application name as a property -->
		            [<appname>HelloWorldCS</appname>]
	

		            <!-- Set the output folder as a property -->
		            [<builtdir>BuiltApp</builtdir>]
		        [</PropertyGroup>]
	

		        [<ItemGroup>]
		            <!-- Specify the inputs by type and file name -->
		            [<CSFile] Include = "consolehwcs1.cs"/>
		        [</ItemGroup>]
	

		        <Target Name = "Compile">
		            <!-- Check whether an output folder exists and create one if necessary -->		
		            [<MakeDir] Directories = "$(builtdir)" Condition = "!Exists('$(builtdir)')" />
	

		            <!-- Run the Visual C# compilation using input files of type [CSFile] -->
		            <CSC 	Sources = "@(CSFile)" [OutputAssembly] = "$(BuiltDir)\$(appname).exe">	
		            <!-- Set the [OutputAssembly] attribute of the CSC task to the name of the executable file that is created -->
	
<Output TaskParameter = "OutputAssembly" ItemName = "EXEFile" />
		            </CSC>
	

		            <!-- Log the file name of the output file -->
		            <Message Text="The output file is @(EXEFile)"/>
		        </Target>
	

		        <Target Name = "Clean">
	
<RemoveDir Directories="$(builtdir)" />
		        </Target>
	

		    </Project>
	

Visual Basic example

		    <Project [DefaultTargets] = "Compile" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
	

		        [<PropertyGroup>]
		            <!-- Set the application name as a property -->
		            [<appname>HelloWorldVB</appname>]
	

		            <!-- Set the output folder as a property -->
		            [<builtdir>BuiltApp</builtdir>]
		        [</PropertyGruop>]
	

		        [<ItemGroup>]
		            <!-- Specify the inputs by type and file name -->
		            [<VBFile] Include = "consolehwvb1.vb"/>
		        [</ItemGroup>]
	

		        <Target Name = "Compile">
		            <!-- Check whether an output folder exists and create one if necessary -->		
		            [<MakeDir] Directories = "$(builtdir)" Condition = "!Exists('$(builtdir)')" />
	

		            <!-- Run the Visual Basic compilation using input files of type [VBFile] -->
		            <VBC Sources = "@(VBFile)" [OutputAssembly] = "$(BuiltDir)\$(appname).exe">
		                <!-- Set the [OutputAssembly] attribute of the VBC task to the name of the executable file that is created -->
	
<Output TaskParameter = "OutputAssembly" ItemName = "EXEFile" />
		            </Task>
	

		            <!-- Log the file name of the output file -->	
		            <Message Text="The output file is @(EXEFile)"/>
		        </Target>
	

		        <Target Name = "Clean">
		            [<RemoveDir] Directories="$(builtdir)" />
		        </Target>
	

		    </Project>
	
Microsoft Communities