Udostępnij za pośrednictwem


MSBuild Conditions

Note

This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

MSBuild 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' Evaluates 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' Evaluates 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. Returns true if the relational evaluation is true. Operands must evaluate to a decimal or hexadecimal number. Hexadecimal numbers must begin with "0x". Note: In XML, the characters < and > must be escaped. The symbol < is represented as <. The symbol > is represented as >.
Exists('stringA') Evaluates 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') Evaluates 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.
! Evaluates to true if the operand evaluates to false.
And Evaluates to true if both operands evaluate to true.
Or Evaluates to true if at least one of the operands evaluates to true.
() Grouping mechanism that evaluates to true if expressions contained inside evaluate to true.
$if$ ( %expression% ), $else$, $endif$ Checks whether the specified %expression% matches the string value of the passed custom template parameter. If the $if$ condition evaluates to true, then its statements are run; otherwise, the $else$ condition is checked. If the $else$ condition is true, then its statements are run; otherwise, the $endif$ condition ends expression evaluation.

For examples of usage, see Visual Studio Project/Item Template Parameter Logic.

See Also

MSBuild Reference
Conditional Constructs
Walkthrough: Creating an MSBuild Project File from Scratch