How to: Reference the Name or Location of the Project File

You can use the name or location of the project in the project file itself without having to create your own property. MSBuild provides reserved properties that reference the project file name and other properties related to the project. For more information on reserved properties, see MSBuild Reserved Properties.

Using the MSBuildProjectName Property

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.

To use the MSBuildProjectName Property

  • Reference the property in the project file with the $() notation, just as you would with any property. For example:

    <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.

Note

Reserved properties cannot be redefined in the project file.

Example

The following example project file references the project name as a reserved property to specify the name for the output.

<Project xmlns="http://scheams.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>

See Also

Reference

MSBuild Reserved Properties

Other Resources

MSBuild