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:

程式Program
%(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\

如果 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) 屬性中指定的專案 IncludeThe 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