MSBuild Conditions
This page is specific to:.NET Framework Version:2.03.03.54.0
.NET Framework General Reference
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

More Examples
Added by:orozcoc
This page needs more examples in order to be more useful.
very small example
Added by:sebastian gomez

I agree with orozoc so here's a very small yet simple example

<Delete Files="$(MSBuildProjectDirectory)\SecuredBins\MyApp.exe" Condition="Exists('$(MSBuildProjectDirectory)\SecuredBins\MyApp.exe')" />
In this case the Delete task will execute after evaluating to true the condition, wich will be tru if the file exists.
Ability to write our own conditions
Added by:Jason Poll
It would be nice if we could write our own conditions. Equals, Greater/Lesser Than, Exists all seem useful primitives, but HasTrailingSlashes really seems like the kind of thing someone would want to write on a need basis, the same way that CaseInsensitiveEquals or Contains might be nice to have (but not necessarily worth including in the core product).
RE: Ability to write our own conditions
Added by:ipashchuk

Currently, a custom condition can be created -- albeit in a roundabout way. First, a custom MSBuild task is created to perform the necessary computation and to return a value. Second, the condition you create evaluates the return value. The example below assumes that you have created a custom task "TestEquals" and appropriately referenced it in your MSBuild script:

<TestEquals Value1="Test" Value2="TEST" ComparisonType="CaseInsensitive">
<Output TaskParameter="Output" PropertyName="EqualsResult" />
</TestEquals>
<Message Condition=" '$(EqualsResult)' == 'true' " Text="Values are equal!" />
<Message Condition=" '$(EqualsResult)' != 'true' " Text="Values are not equal!" />


© 2009 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View