共用方式為


如何:從組建中排除檔案

在專案檔中,您可以使用萬用字元,併入一個目錄或巢狀目錄集合中的所有檔案,做為組建 (Build) 的輸入。 不過,您也許不想併入目錄中的某個檔案或巢狀目錄集合中的某個目錄來做為組建的輸入。 那麼,您可以明確地從輸入清單中排除那個檔案或目錄。 此外,您也許只想在特定的條件下,才要併入專案中的某個檔案, 此時,您可以明確地宣告要將這個檔案併入組建中的各種條件。

從組建的輸入中排除檔案或目錄

項目清單是組建的輸入檔。 您可以使用 Include 屬性 (Attribute),以分別的或整個群組的方式來宣告要併入的項目。 例如:

<CSFile Include="Form1.cs"/>
<CSFile Include ="File1.cs;File2.cs"/>
<CSFile Include="*.cs"/>
<JPGFile Include="Images\**\*.jpg"/>

如果您使用萬用字元,併入一個目錄或一組巢狀目錄中的所有檔案當做組建的輸入,您也許不想併入該目錄中的一個或多個檔案或該組巢狀目錄中的某個目錄。 若要從項目清單中排除某個項目,請使用 Exclude 屬性。

若要併入 Form2 以外的所有 .cs 或 .vb 檔案

  • 使用下列其中一個 Include 和 Exclude 屬性:

    <CSFile Include="*.cs" Exclude="Form2.cs"/>
    

    -或-

    <VBFile Include="*.vb" Exclude="Form2.vb"/>
    

若要併入 Form2 和 Form3 以外的所有 .cs 或 .vb 檔案

  • 使用下列其中一個 Include 和 Exclude 屬性:

    <CSFile Include="*.cs" Exclude="Form2.cs;Form3.cs"/>
    

    -或-

    <VBFile Include="*.vb" Exclude="Form2.vb;Form3.vb"/>
    

若要併入 Images 目錄中子目錄的所有 .jpg 檔案 (但 Version2 目錄內的檔案除外)

  • 使用下列 Include 和 Exclude 屬性:

    <JPGFile
        Include="Images\**\*.jpg"
        Exclude = "Images\**\Version2\*.jpg"/>
    
    注意事項注意事項

    您必須指定這兩個屬性的路徑。如果您在 Include 屬性中使用絕對路徑來指定檔案位置,那麼在 Exclude 屬性中也應該使用絕對路徑。如果在 Include 屬性中使用相對路徑,那麼在 Exclude 屬性中同樣要使用相對路徑。

使用條件從組建的輸入中排除檔案或目錄

舉例來說,如果您想將某些項目併入到偵錯組建而不是發行的組建 (Release Build) 內,則可使用 Condition 屬性,指定要併入項目的條件。

若只想在發行的組建內併入 Formula.vb 檔案

  • 以與下列類似的方式使用 Condition 屬性:

    <Compile
        Include="Formula.vb"
        Condition=" '$(Configuration)' == 'Release' " />
    

範例

下列程式碼範例會建置 (Build) 一個包含目錄中除 Form2.cs 以外所有 .cs 檔案的專案。

<Project DefaultTargets="Compile"
    xmlns="https://schemas.microsoft.com/developer/msbuild/2003" >

    <PropertyGroup>
        <builtdir>built</builtdir>
    </PropertyGroup>

    <ItemGroup>
        <CSFile Include="*.cs Exclude="Form2.cs"/>

        <Reference Include="System.dll"/>
        <Reference Include="System.Data.dll"/>
        <Reference Include="System.Drawing.dll"/>
        <Reference Include="System.Windows.Forms.dll"/>
        <Reference Include="System.XML.dll"/>
    </ItemGroup>

    <Target Name="PreBuild">
        <Exec Command="if not exist $(builtdir) md $(builtdir)"/>
    </Target>

    <Target Name="Compile" DependsOnTargets="PreBuild">
        <Csc Sources="@(CSFile)"
            References="@(Reference)"
            OutputAssembly="$(builtdir)\$(MSBuildProjectName).exe"
            TargetType="exe" />
    </Target>
</Project>

請參閱

工作

如何:選取要建置的檔案

概念

MSBuild 項目

其他資源

MSBuild