Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
MSBuild provides reserved properties that reference the project file name and other properties related to the project. You can use the name or location of the project in the project file itself without having to create your own property. For more information on reserved properties, see MSBuild reserved and well-known properties.
Prerequisites
A Visual Studio project that builds with MSBuild.
Use reserved project properties
MSBuild provides some reserved properties that you can use in your project files without defining them each time. For example, the reserved property MSBuildProjectName
provides a reference to the project file name. The reserved property MSBuildProjectDirectory
provides a reference to the project file location.
An advantage of using the reserved property is that any changes to the project file name are incorporated automatically. The next time you build the project, the output file and other file names that use the property automatically update to the new name.
To use the project properties, reference the property in the project file with the $()
notation, just as you would any property. For example:
<CSC Sources = "@(CSFile)"
OutputAssembly = "$(MSBuildProjectName).exe"/>
</CSC>
For information about using special characters in file or project references, see MSBuild special characters.
Note
You can't redefine reserved properties in the project file.
Use MSBuildProjectName to specify output file name
The following example project file references the project name as a reserved property to specify the name for the output.
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
DefaultTargets = "Compile">
<!-- Specify the inputs -->
<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>
Use MSBuildProjectDirectory to create the full path to a file
The following example project file uses the MSBuildProjectDirectory
reserved property to create the full path to a file in the project file location. The example uses the property function syntax to call the static .NET Framework method System.IO.Path.Combine.
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- Build the path to a file in the root of the project -->
<PropertyGroup>
<NewFilePath>$([System.IO.Path]::Combine($(MSBuildProjectDirectory), `BuildInfo.txt`))</NewFilePath>
</PropertyGroup>
</Project>