您可以使用项目文件自身中的项目名称或位置,不必创建您自己的属性。 MSBuild 提供保留属性,这些属性引用项目文件名和与项目相关的其他属性。 有关保留属性的更多信息,请参见 MSBuild 保留属性。
使用 MSBuildProjectName 属性
MSBuild 提供了一些保留属性,您可以在项目文件中使用这些属性,而不用每次都定义它们。 例如,保留属性 MSBuildProjectName 提供对项目文件名的引用。
使用 MSBuildProjectName 属性
- 与任何属性一样,使用 $() 表示法在项目文件中引用此属性。 例如: - <CSC Sources = "@(CSFile)" OutputAssembly = "$(MSBuildProjectName).exe"/> </CSC>
使用保留属性的一个优点是,对项目文件名的任何更改都自动合并到文件中。 下次生成项目时,输出文件将自动带有新名称,您不需要采取进一步的操作。
提示
无法在项目文件中重新定义保留属性。
示例
下面的示例项目文件将项目名称作为保留属性引用,以指定输出文件的名称。
<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>