选择要生成的文件
如果生成包含多个文件的项目,可以在项目文件中分别列出每个文件,也可以使用通配符将一个目录或一组嵌套目录中的所有文件都包括进去。
指定输入
项表示某个生成的输入。 有关项的详细信息,请参阅项。
若要包括某个生成的文件,必须在 MSBuild 项目文件的项列表中包括这些文件。 通过逐个包括文件或使用通配符同时包括许多文件,可以将多个文件添加到项列表中。
逐个声明各个项
使用
Include
属性,如下所示:<CSFile Include="form1.cs"/>
或
<VBFile Include="form1.vb"/>
注意
如果项集合中的项不在项目文件所在的同一目录中,则必须指定项的完整路径或相对路径。 例如:
Include="..\..\form2.cs"
。
声明多个项
使用
Include
属性,如下所示:<CSFile Include="form1.cs;form2.cs"/>
或
<VBFile Include="form1.vb;form2.vb"/>
使用通配符指定输入
还可以使用通配符以递归方式将子目录中的所有文件或某些特定文件包括在某个生成的输入中。 有关通配符的详细信息,请参阅通配符
下面的示例基于一个项目,该项目包含下列目录和子目录中的图形文件,项目文件位于 Project 目录中:
Project\Images\BestJpgs
Project\Images\ImgJpgs
Project\Images\ImgJpgs\Img1
包括 Images 目录和子目录中的所有 .jpg 文件
使用下面的
Include
属性:Include="Images\**\*.jpg"
包括所有以“img”开头的 .jpg 文件
使用下面的
Include
属性:Include="Images\**\img*.jpg"
包括目录中名称以“jpgs”结尾的所有文件
使用以下
Include
属性之一:Include="Images\**\*jpgs\*.*"
或
Include="Images\**\*jpgs\*"
将项传递给任务
在项目文件中,可以在任务中使用 @() 表示法,指定整个项列表作为生成的输入。 无论是分别列出所有文件还是使用通配符,均可以使用此表示法。
使用所有 Visual C# 或 Visual Basic 文件作为输入
使用
Include
属性,如下所示:<CSC Sources="@(CSFile)">...</CSC>
或
<VBC Sources="@(VBFile)">...</VBC>
注意
必须将通配符与项配合使用来指定生成的输入,不能使用 MSBuild 任务(如 Csc 或 Vbc)中的 Sources
属性来指定输入。 下面的示例在项目文件中无效:
<CSC Sources="*.cs">...</CSC>
示例 1
以下代码示例演示的项目单独包括所有输入文件。
<Project DefaultTargets="Compile"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003" >
<PropertyGroup>
<Builtdir>built</Builtdir>
</PropertyGroup>
<ItemGroup>
<CSFile Include="Form1.cs"/>
<CSFile Include="AssemblyInfo.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>
示例 2
以下代码示例使用通配符来包括所有 .cs 文件。
<Project DefaultTargets="Compile"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003" >
<PropertyGroup>
<builtdir>built</builtdir>
</PropertyGroup>
<ItemGroup>
<CSFile Include="*.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>
相关内容
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈