views:

509

answers:

3

How can I force Visual Studio 2010 to use MSBuild 3.5 instead of the new 4.0 ?

+1  A: 

You can target different versions of runtime from the project properties dialog:

alt text

Not sure if modifying the build rules to use MsBuild 3.5 for C#/VB.NET targets is a good idea.

(The below change is unnecessary. Using .NET 4 targets file has no problem at all.) Edit Try to open the c# project with a text editor and modify this line:

<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

to this:

<Import Project="C:\Windows\Microsoft.NET\Framework\v3.5\Microsoft.CSharp.targets" />
Igor Zevaka
It doesn't help, as MSBuild 4.0 uses Framework 4.0. We need to target MSBuild 3.5
HeavyWave
See edit, that might help. Not sure what's going to happen to the system references, might need to point those to v3.5 of the assembies if they got converted during Vs2008->VS2010 conversion.
Igor Zevaka
That won't change the actual executable used either.
HeavyWave
interesting....
Igor Zevaka
@HeavyWave, what you complain "It doesn't help, as MSBuild 4.0 uses Framework 4.0. We need to target MSBuild 3." is a misunderstanding. Though MSBuild 4 relies on .NET 4, but if you ask it to build your project against earlier .NET versions, you will see the output only depends on old versions, not .NET 4. You don't even need to edit the <Import> tag.
Lex Li
The output yes, but MSBuild itself references .Net 4.
HeavyWave
Visual Studio 2010 also references .NET 4 assemblies, but it has nothing to do with build results right? Then why you cannot use MSBuild 4?
Lex Li
A: 

Why do you want that? Is there anything 3.5 capable but 4.0 not?

If your real concern is that when the code is checked out to a machine that does not have .NET 4 installed, you can use MSBuild 3.5 to build the projects (csproj). You need to create a solution file for MSBuild 3.5 as it does not accept the sln file of VS2010.

When MSBuild comes across ToolsVersion="4.0" it will treat it as ToolsVersion="3.5" and then everything continues to work.

My open source project #SNMP utilizes such tricks so the source code can be built on .NET 4, .NET 3.5 and Mono.

Lex Li
To use VS 2010 with a project that won't work with MSBuild 4.0.
HeavyWave
@Heavy: why does the project not work with MSBUILD 4.0?
John Saunders
+2  A: 

I think you can't change the version of MSBuild used within Visual Studio without a dirty hack.

ToolsVersion

The version of MSBuild tasks, targets, and tools used by MSBuild is determined by the ToolsVersion attribute in the project file.

  • Visual Studio 2008 uses ToolsVersion="3.5"
  • Visual Studio 2010 uses ToolsVersion="4.0"

Problem : If you change the ToolsVersion manually to 3.5 on the project file, Visual Studio 2010 will automatically upgrade the project file's ToolsVersion to 4.0

The hack

The solution is to set your project ToolsVersion to 3.6 (or whatever or 4.0 if you really want to override MSBuild 4.0) and then in the registry to create the following key :

  • Registry Hive: \HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions\3.6
  • String Key Name: MSBuildToolsPath
  • String Key Value: .NET Framework 3.5 Install Path (C:\Windows\Microsoft.NET\Framework\v3.5)

On machine where ToolsVersion is unknown, the default ToolsVersion will be used.

More information on custom toolset definition.

madgnome
On machines MSBuild 3.5 is used to build VS2010 csproj, MSBuild 3.5 simply treats ToolsVersion="4.0" as ToolsVersion="3.5". So VS2010 project files can still be built by MSBuild 3.5 in some scenarios (especially when you ask VS2010 to build against .NET 3.5 instead of 4).
Lex Li