Raffaeu
Raffaeu

Reputation: 6973

MSBuild, conditional NET runtime

I have a set of tools that need to be deployed on NET 3.5 or NET 4.0 depending on an MsBuild condition. At the moment we would like to change the project file of those utilities to handle this. We are aware that we can do something like this:

<TargetFrameworkVersion Condition="">v3.5</TargetFrameworkVersion>

What is not clear for us is how can we specify different versions of NET depending on the condition. The condition property is an int that returns a number, between 1 and 4 and depending on that value we should target a different NET framework and of course change also this property in the app.config

<startup>
   <supportedRuntime version="v2.0.50727"/>
</startup>

I want to know what is the right way of handling this type of problem.

Upvotes: 1

Views: 459

Answers (1)

RinoTom
RinoTom

Reputation: 2326

You can give the condition like this one after other in your MSBuild file.

<TargetFrameworkVersion Condition="$(ConditionProperty) == '1'">v1.1.xxxx</TargetFrameworkVersion>
<TargetFrameworkVersion Condition="$(ConditionProperty) == '2'">v2.0.xxxx</TargetFrameworkVersion>
<TargetFrameworkVersion Condition="$(ConditionProperty) == '3'">v3.5.xxxx</TargetFrameworkVersion>
<TargetFrameworkVersion Condition="$(ConditionProperty) == '4'">v4.0.xxxx</TargetFrameworkVersion>

Accordingly you can write the code to change the value of

<startup>
   <supportedRuntime version="v2.0.50727"/>
</startup>

in your app.cofig file as well by using the value of the variable $(TargetFrameworkVersion) using the code below:

<XmlUpdate XmlFileName="app.config"
           XPath="//startup/supprtedRuntime[@version]"
           Value="$(TargetFrameworkVersion)" />

Upvotes: 3

Related Questions