MSBuild 常见的项元数据MSBuild well-known item metadata

项元数据是附加到项的值。Item metadata are values attached to items. 有些是由 MSBuild 在创建项时分配给项的,但你也可以定义所需的任何元数据。Some are assigned by MSBuild to items when items are create, but you can also define any metadata you need. 一些用户定义的元数据值对于 MSBuild、特定任务或 SDK(如 .NET SDK)具有意义。Some user-defined metadata values have meaning to MSBuild, specific tasks tasks, or SDKs such as the .NET SDK.

本文的第一个表格介绍了创建每个项时分配给该项的元数据。The first table in this article describes the metadata assigned to every item upon creation. 下表显示了一些对于 MSBuild 有意义的可选元数据,你可以定义这些元数据来控制生成行为。The next table shows some optional metadata that has meaning for MSBuild, which you can define to control build behavior. 在每个示例中,以下项声明用于将文件 C:\MyProject\Source\Program.cs 包含在项目中。In each example, the following item declaration was used to include the file C:\MyProject\Source\Program.cs in the project.

<ItemGroup>
    <MyItem Include="Source\Program.cs" />
</ItemGroup>
项元数据Item metadata 描述Description
%(FullPath)%(FullPath) 包含项的完整路径。Contains the full path of the item. 例如:For example:

C:\MyProject\Source\Program.csC:\MyProject\Source\Program.cs
%(RootDir)%(RootDir) 包含项的根目录。Contains the root directory of the item. 例如:For example:

C:\C:\
%(Filename)%(Filename) 包含项的文件名,但不包含扩展名。Contains the file name of the item, without the extension. 例如:For example:

ProgramProgram
%(Extension)%(Extension) 包含项的文件扩展名。Contains the file name extension of the item. 例如:For example:

.cs.cs
%(RelativeDir)%(RelativeDir) 包含 Include 属性中指定的路径,直到最后的反斜杠 (\)。Contains the path specified in the Include attribute, up to the final backslash (\). 例如:For example:

Source\Source\

如果 Include 属性是完整路径,则 %(RelativeDir) 从根目录 %(RootDir) 开始。If the Include attribute is a full path, %(RelativeDir) begins with the root directory %(RootDir). 例如:For example:

C:\MyProject\Source\C:\MyProject\Source\
%(Directory)%(Directory) 包含项的目录,但不包含根目录。Contains the directory of the item, without the root directory. 例如:For example:

MyProject\Source\MyProject\Source\
%(RecursiveDir)%(RecursiveDir) 如果 Include 属性包含通配符 **,则此元数据将指定代替通配符的路径的一部分。If the Include attribute contains the wildcard **, this metadata specifies the part of the path that replaces the wildcard. 有关通配符的详细信息,请参阅如何:选择要生成的文件For more information on wildcards, see How to: Select the files to build.

如果文件夹 C:\MySolution\MyProject\Source\ 包含文件 Program.cs,并且该项目文件包含此项:If the folder C:\MySolution\MyProject\Source\ contains the file Program.cs, and if the project file contains this item:

<ItemGroup>

<MyItem Include="C:\**\Program.cs" />

</ItemGroup>

%(MyItem.RecursiveDir) 的值为 MySolution\MyProject\Source\。then the value of %(MyItem.RecursiveDir) would be MySolution\MyProject\Source\.
%(Identity)%(Identity) Include 属性中指定的项。The item specified in the Include attribute. 例如:For example:

Source\Program.csSource\Program.cs
%(ModifiedTime)%(ModifiedTime) 包含上一次修改项的时间戳。Contains the timestamp from the last time the item was modified. 例如:For example:

2004-07-01 00:21:31.5073316
%(CreatedTime)%(CreatedTime) 包含创建项的时间戳。Contains the timestamp from when the item was created. 例如:For example:

2004-06-25 09:26:45.8237425
%(AccessedTime)%(AccessedTime) 包含上一次访问项的时间戳。Contains the timestamp from the last time the item was accessed.

2004-08-14 16:52:36.3168743

请参阅See also