Reference the Name or Location of the Project File in the Project File
You can use the name or location of the project in the project file itself without embedding it.
MSBuild provides a reserved property that references the project file name and other reserved properties related to the project.
In this example, the "Hello World" project file references the project name as a reserved property to specify the name for the output.
*MSBuildProjectName*
The
Property element can be used to define a variable that is referenced several times in the project file. For example, the property "appname" can be declared:
<appname>HelloWorldCS</appname> then referenced in any element attribute value to specify the name of the output file:
<CSC
Sources = "@(CSFile)"
OutputAssembly = "$(appname).exe"/>
</CSC>
However,
MSBuild also provides some reserved properties that you can use in your project files without defining them each time. The reserved property
*MSBuildProjectName* can be used to reference the project file name. For example, you can specify that the name of the output assembly is the project name:
<CSC
Sources = "@(CSFile)"
OutputAssembly = "$(MSBuildProjectName).exe"/>
</CSC>
An advantage of using a reserved property is that any changes to the project file name are incorporated automatically. The next time that you build the project, the output file will have the new name with no further action required on your part.
''Reserved properties cannot be redefined in the project file. For example, the following property cannot be declared in a project file:
<
*MSBuildProjectName*>MyApp</
*MSBuildProjectName*>''
Reserved Properties
MSBuild provides the following reserved properties:
*
'''MSBuildProjectDirectory
''' — The absolute path of the directory where the project file is located, for example,
*C:\MyCompany\MyProduct* *
'''MSBuildProjectFilename
''' — The complete file name of the project file, including the file name extension, for example,
*MyApp.proj* *
'''MSBuildProjectExtension
''' — The file name extension of the project file, including the period, for example, .proj
*
'''MSBuildProjectFullPath
''' — The absolute path and complete file name of the project file, for example,
*C:\MyCompany\MyProduct\MyApp.proj* *
'''MSBuildProjectName
''' — The file name of the project file without the file name extension, for example,
*MyApp* *
'''MSBuildBinPath
''' — The absolute path of the directory where the
MSBuild binaries that are currently being used are located, for example, C:\Windows\Microsoft.Net\Framework\v2.0.31230.00. This property is useful if you need to refer to files in the
MSBuild directory.
*
'''MSBuildProjectDefaultTargets
''' — The
DefaultTargets of the current project.
*
'''MSBuildExtensionsPath
''' — The path to the
MSBuild folder under the current Program Files folder. Vendors can install their tasks and targets in this location or below, and use this property to find them.
Building a Project
To build the Visual C# or Visual Basic "Hello World" project from the command line, navigate to the directory that contains the project file and type:
msbuild consolehwcs1.proj
or
msbuild consolehwvb1.proj
Project Files
Download Code - C#Download Code - VB Visual C# example <Project
xmlns=http://scheams.microsoft.com/developer/msbuild/2003
DefaultTargets = "Compile">
<!-- Specify the inputs by type and file name -->
<ItemGroup> <CSFile Include = "consolehwcs1.cs"/>
</ItemGroup> <Target Name = "Compile">
<!-- Run the Visual C# compilation using input files of type
CSFile -->
<CSC Sources = "@(CSFile)"
OutputAssembly = "$(MSBuildProjectName).exe" >
<!-- Set the
OutputAssembly attribute of the CSC task to the name of the project -->
<Output
TaskParameter = "OutputAssembly"
ItemName = "EXEFile" />
</CSC>
<!-- Log the file name of the output file -->
<Message Text="The output file is @(EXEFile)"/>
</Target>
</Project>
Visual Basic example <Project
xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
DefaultTargets = "Compile">
<!-- Set the application name as a property -->
<PropertyGroup> <appname = "HelloWorldVB"/>
</PropertyGroup> <!-- Specify the inputs by type and file name -->
<ItemGroup> <VBFile Include = "consolehwvb1.vb"/>
</ItemGroup> <Target Name = "Compile">
<!-- Run the Visual Basic compilation using input files of type
VBFile -->
<VBC
Sources = "@(VBFile)"
Include = "$(MSBuildProjectName).exe">
<!-- Set the
OutputAssembly attribute
of the VBC task to the name
of the project -->
<Output
TaskParameter="OutputAssembly"
ItemName="EXEFile" />
</VBC>
<!-- Log the file name of the output file -->
<Message Text="The output file is @(EXEFile)"/>
</Target>
</Project>