Using MSBuild with Multiple Source Files

A tutorial example is provided on how to specify the 'Csc' command patch name and output file name using properties.

To resolve the problem in the previous tutorial, we need to inform MSBuild where the "Csc" command is located. This can be done by using the "ToolPath" attribute: ToolPath="C:\windows\Microsoft.NET\Framework\v4.0.30319".

Or we can define a property called "NET" as a variable to hold the path value, as shown in the next project file example:

<!-- SwapTest.proj
// Copyright (c) 2016 HerongYang.com. All Rights Reserved.
-->
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
     <NET>C:\windows\Microsoft.NET\Framework\v4.0.30319</NET>
     <AssemblyName>SwapTest</AssemblyName>
  </PropertyGroup>
  <ItemGroup>
    <Compile Include="Util.cs" />
    <Compile Include="SwapTest.cs" />
  </ItemGroup>
  <Target Name="Build">
    <Csc ToolPath="$(NET)" Sources="@(Compile)"
       OutputAssembly="$(AssemblyName).exe"/>
  </Target>
</Project>

More features are used in this MSBuild project file:

Make sure that SwapTest.cs and Util.cs from earlier tutorials are in the current folder. Then run MSBuild with the above project file:

C:\herong>set "NET=\windows\Microsoft.NET\Framework\v4.0.30319"

C:\herong>%NET%\msbuild SwapTest.proj
Microsoft (R) Build Engine version 4.6.1055.0
[Microsoft .NET Framework, version 4.0.30319.42000]
Copyright (C) Microsoft Corporation. All rights reserved.

Build started 10:39:18 PM.
Project "C:\herong\SwapTest.proj" on node 1 (default targets).
Build:
  C:\windows\Microsoft.NET\Framework\v4.0.30319\Csc.exe
  /out:SwapTest.exe Util.cs SwapTest.cs

Done Building Project "C:\herong\SwapTest.proj" (default targets).

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:00.35

Ok. The MSBuild tool did what we expected.

Table of Contents

 About This Book

 Introduction of C# (C Sharp)

 Data Type and Variables

 Logical Expressions and Conditional Statements

 Arrays and Loop Statements

 Data Type Features

 Floating-Point Data Types

 Passing Parameters to Methods

 Execution Environment Class

 Visual C# 2010 Express Edition

 Class Features

 C# Compiler and Intermediate Language

 Compiling C# Source Code Files

MSBuild - Microsoft Build Engine

 What Is MSBuild?

 First MSBuild Project File - Hello.proj

Using MSBuild with Multiple Source Files

 Memory Usages of Processes

 Multithreading in C#

 Async Feature from C# 5

 System.IO.FileInfo Class

 System.Diagnostics.FileVersionInfo Class

 WPF - Windows Presentation Foundation

 Partial Classes and Partial Methods

 Archived Tutorials

 References

 Full Version in PDF/ePUB