First, little explanation for what prompted me to write this post… 

 

It came to my attention that some teams dedicate a build server for each Visual Studio/.NET framework combination, i.e. they don’t upgrade Visual Studio (or the build tools) because they need to build certain projects (developed using the latest version of VS but utilizing only backward compatible features) to run on current and previous versions of the .NET runtime…  So, they build the source on different build servers targeting required runtime.

 

The msbuild process is so flexible, that this is absolutely not necessary!  Below are simple steps you can implement and have ability to choose your target runtime at build time...  The example below adds ability to target .NET 1.1.4322 runtime, but you can use same process to target other runtimes…

 

1.  On VisualStudio toolbar click ‘Solution Configuration’ dropdown list (the one with Debug, Release, Configuration Manager... items, usually found right after the 'Start Debugging' icon), and select 'Configuration Manager...' option.

2.  Click 'Active Solution Configuration' dropdown list and click <New...> item

3.  In the 'Name' textbox, type in 'Debug .NET 1.1'

4.  In the 'Copy settings from' dropdown list, select 'Debug' item and click Ok

5.  Repeat steps 2 through 4 for Release .NET 1.1 build

6.  Close solution

7.  Open your project(s) file (.csproj) using a text editor (e.g. Notepad)

8.  Towards the end of the file, right before 

 

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

 

line, add the following section:

 

<PropertyGroup Condition=" '$(Configuration)' == 'Debug .NET 1.1'  OR '$(Configuration)' == 'Release .NET 1.1' " >   

<TargetFrameworkVersion>v1.1</TargetFrameworkVersion>

    <TargetFrameworkDirectory>c:\windows\Microsoft.NET\Framework\v1.1.4322</TargetFrameworkDirectory>

    <ReferencePath>c:\windows\Microsoft.NET\Framework\v1.1.4322</ReferencePath>

</PropertyGroup>

 

9.  Reopen your solution.  Now, you should have an option to compile for current .NET version (debug or release) or .NET 1.1 by selecting your choice in the Solution Configuration dropdown list.  To see what versions of referenced components were loaded, you can use the following code:

 

System.Text.StringBuilder sb = new System.Text.StringBuilder();

foreach (System.Reflection.AssemblyName n in System.Reflection.Assembly.GetExecutingAssembly().GetReferencedAssemblies())

{

    sb.AppendLine(n.FullName);

}

MessageBox.Show(sb.ToString());