Share via


HOW TO:參考專案檔的名稱或位置

更新:2007 年 11 月

您可以使用專案檔本身的專案名稱和位置,不需要建立自己的屬性。MSBuild 提供了保留的屬性,這些屬性會參考專案檔名稱和與專案相關的其他屬性。如需保留屬性的詳細資訊,請參閱 MSBuild 保留屬性

使用 MSBuildProjectName 屬性

MSBuild 提供了幾個保留的屬性,您可以直接用在專案檔中,不需要每次定義。例如,保留屬性 MSBuildProjectName 提供了專案檔名的參考。

若要使用 MSBuildProjectName 屬性

  • 在專案檔中使用 $() 標記法參考此屬性,就會使用其他屬性一樣。例如:

    <CSC Sources = "@(CSFile)" 
        OutputAssembly = "$(MSBuildProjectName).exe"/>
    </CSC>
    

使用保留屬性的好處,是專案檔名的任何變更都會自動併入。下次建置 (Build) 專案時,輸出檔就會使用新名稱,您完全不需要採取任何行動。

注意事項:

保留的屬性無法在專案檔中重新定義。

範例

下列範例專案檔會以保留的屬性參考專案名稱,以指定輸出的名稱。

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

請參閱

概念

MSBuild

參考

MSBuild 保留屬性