MSBuild 條件MSBuild conditions

MSBuildMSBuild 支援一組只要允許 Condition 屬性就能套用的特定條件。supports a specific set of conditions that can be applied wherever a Condition attribute is allowed. 下表說明這些條件。The following table explains those conditions.

條件Condition 說明Description
'stringA' == 'stringB''stringA' == 'stringB' 如果 stringA 等於 stringB,即會評估為 trueEvaluates to true if stringA equals stringB.

例如:For example:

Condition="'$(CONFIG)'=='DEBUG'"

不需要以單引號括住簡單的英數字元字串或布林值。Single quotes are not required for simple alphanumeric strings or boolean values. 不過,需要使用單引號括住空白值。However, single quotes are required for empty values.
'stringA' != 'stringB''stringA' != 'stringB' 如果 stringA 不等於 stringB,即會評估為 trueEvaluates to true if stringA is not equal to stringB.

例如:For example:

Condition="'$(CONFIG)'!='DEBUG'"

不需要以單引號括住簡單的英數字元字串或布林值。Single quotes are not required for simple alphanumeric strings or boolean values. 不過,需要使用單引號括住空白值。However, single quotes are required for empty values.
<、>、<=、>=<, >, <=, >= 評估運算元的數值。Evaluates the numeric values of the operands. 如果關聯式評估為 true,即會傳回 trueReturns true if the relational evaluation is true. 運算元必須評估為十進位或十六進位數字。Operands must evaluate to a decimal or hexadecimal number. 十六進位數字必須以 "0x" 開頭。Hexadecimal numbers must begin with "0x". 注意: 在 XML 中,必須逸出字元 <>Note: In XML, the characters < and > must be escaped. 符號 < 是以 &lt; 表示。The symbol < is represented as &lt;. 符號 > 是以 &gt; 表示。The symbol > is represented as &gt;.
Exists('stringA')Exists('stringA') 如果有名稱為 stringA 的檔案或資料夾存在,即會評估為 trueEvaluates to true if a file or folder with the name stringA exists.

例如:For example:

Condition="!Exists('$(builtdir)')"

不需要以單引號括住簡單的英數字元字串或布林值。Single quotes are not required for simple alphanumeric strings or boolean values. 不過,需要使用單引號括住空白值。However, single quotes are required for empty values.
HasTrailingSlash ('stringA')HasTrailingSlash('stringA') 如果指定的字串包含尾端反斜線 (\) 或斜線 (/) 字元,即會評估為 trueEvaluates to true if the specified string contains either a trailing backward slash (\) or forward slash (/) character.

例如:For example:

Condition="!HasTrailingSlash('$(OutputPath)')"

不需要以單引號括住簡單的英數字元字串或布林值。Single quotes are not required for simple alphanumeric strings or boolean values. 不過,需要使用單引號括住空白值。However, single quotes are required for empty values.
!! 如果運算元評估為 false,即會評估為 trueEvaluates to true if the operand evaluates to false.
And 如果這兩個運算元都評估為 true,即會評估為 trueEvaluates to true if both operands evaluate to true.
Or 如果至少有一個運算元評估為 true,即會評估為 trueEvaluates to true if at least one of the operands evaluates to true.
()() 如果內部包含的運算式評估為 true,即會評估為 true 的群組機制。Grouping mechanism that evaluates to true if expressions contained inside evaluate to true.
$if$ ( %expression% )、$else$、$endif$$if$ ( %expression% ), $else$, $endif$ 檢查指定的 %expression% 是否符合所傳遞自訂範本參數的字串值。Checks whether the specified %expression% matches the string value of the passed custom template parameter. 如果 $if$ 條件評估為 true,即會執行它的陳述式,否則會檢查 $else$ 條件。If the $if$ condition evaluates to true, then its statements are run; otherwise, the $else$ condition is checked. 如果 $else$ 條件為true,即會執行它的陳述式,否則 $endif$ 條件會結束運算式評估。If the $else$ condition is true, then its statements are run; otherwise, the $endif$ condition ends expression evaluation.

如需使用方式的範例,請參閱 Visual Studio project/item template parameter logic (Visual Studio 專案/項目範本參數邏輯)。For examples of usage, see Visual Studio project/item template parameter logic.

另請參閱See also