22

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?

Chris Catignani
  • 5,040
  • 16
  • 42
  • 49
Fredefl
  • 1,391
  • 2
  • 17
  • 33
  • 7
    Why man, why? If your code compile in 3.5, this means you don't use any 4.0 features. Why target 4.0 then? – Rodrigo Jun 16 '11 at 15:04
  • 8
    There are legitimate uses for this. Libraries do this. It's less headache for the library's consumers. I admit that's not what he is doing, but having this question answered is personally useful to me. – jnm2 Jun 16 '11 at 15:15
  • 2
    Having the same issue, for me it is the reason that the application I'm building only requires .NET 2 (which I'm using to get the broadest reach on audience), and I want another version to be able to submit to Intel's AppUp.com plattform, which only allows .NET 3.5 application. – Uwe Keim Jul 16 '11 at 07:22
  • 1
    I believe [c-conditional-compilation-and-framework-targets](http://stackoverflow.com/questions/2923210/c-conditional-compilation-and-framework-targets/2928835#2928835) is a better answer. – Arthur Aug 11 '13 at 15:19

2 Answers2

19

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.

CodeNaked
  • 40,753
  • 6
  • 122
  • 148
  • 2
    Brilliant answer. Thanks very much for this as it saved me much heartache trying to do things in a far more complex way. – David Arno Jun 04 '13 at 20:04
4

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

Felipe Sabino
  • 17,825
  • 6
  • 78
  • 112