Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile to two .NET Frameworks at once

Tags:

I have a program in C#, and I want to make it compile to two different .NET Framework versions.

So when I press the Compile button, it makes, for example, "ComputerInfo3.exe" and "ComputerInfo4.exe".

I want to use version 3.5 and 4 of the .NET Framework. I use Visual Studio and C#. Is this possible?

like image 668
Fredefl Avatar asked Jun 16 '11 14:06

Fredefl


People also ask

Can I run multiple versions of .NET Framework?

It is safe to install multiple versions of the . NET Framework on your computer.

Can you have multiple target frameworks?

For SDK-style projects, you can configure support for multiple targets frameworks (TFM) in your project file, then use dotnet pack or msbuild /t:pack to create the package. nuget.exe CLI does not support packing SDK-style projects, so you should only use dotnet pack or msbuild /t:pack .

Can you mix .NET core and .NET framework?

At this point, if your NuGet dependencies are compatible with both your netcore and netframework targets you may be done! The big news here is that most of your NuGet dependencies are compatible with both. All of the most downloaded NuGet packages are either multi-targeted, or have packages for each target.


2 Answers

Your best bet would be to create two separate csproj files, one that targets 3.5 and one that targets 4.0, and include them in the same solution. You can add all the files from one project to the other project. If you link the file, instead of regular add, then any updates you make in the file will be applied to both projects.

You will most likely run into issues if you try to have the projects in the same location, due to how Visual Studio maintains temporary build files. So you'd need to keep them in separate folders.

Finally, if you build an executable that targets 3.5, then it can be run as-is on .NET 4. So in general you should not need to build two versions of the EXE.

like image 63
CodeNaked Avatar answered Nov 07 '22 06:11

CodeNaked


You could also use nAnt for automate builds and then you can configure several targets, each one for each framework

like image 41
Felipe Sabino Avatar answered Nov 07 '22 07:11

Felipe Sabino