MSBuild Conditions

Switch View :
ScriptFree
.NET Framework 4
MSBuild Conditions

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".

NoteNote
In XML, the characters < and > must be escaped. The symbol < is represented as &lt;. The symbol > is represented as &gt;.

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.

See Also

Reference

Other Resources

Community Content

Barrie K
Using 'Exists' condition with wildcards
I could not get the Exists condition to work with wildcards.
i.e.
Condition = "Exists('%(Outputs.RelativeDir)\*.dll') "
always evaluated to false even when there were .dlls in the Outputs folder

So I used this form instead: -

Condition="'$([System.IO.Directory]::GetFiles(%(Outputs.RelativeDir),*.dll).Length)'!='0'"

Antek B
A work around for the problem described below...
$0<GacFiles Include="$(SnapshotDir)\**\*_gac.txt" />$0 $0<Files1 Include="$(SnapshotDir)\**\*" Condition=" Exists('%(CandidateGacFiles.RootDir)%(CandidateGacFiles.Directory)') " />$0

Antek B
How does Exist evaluate exactly?

Given the following two lines of code I'd expect to get the same results, unfortunately that isn't the case.

<Files1 Include="$(OutputDir)\**\*_gac.txt" Condition=" Exists('$(OutputDir)\**\*_gac.txt') " />

<Files2 Include="$(OutputDir)\**\*_gac.txt" />

Files1 always returns an empty ItemGroup and given the explenation of Exists in Conditions I can't understand why.


Antek B
Why don't both statements produce the same results?
<ItemGroup>
<Files1 Include="$(OutputDir)\**\*_gac.txt" Condition=" Exists('$(OutputDir)\**\*_gac.txt') " />
<Files2 Include="$(OutputDir)\**\*_gac.txt" />
</ItemGroup>