8

I write a WPF app, that all source code compatible with both .NET 3.5 and 4.5. That mean if I change .NET target in the Properties to either 3.5 or 4.5, it will complie and run well without any error.
I wonder there is anyway to my my app file can run on both 3.5 and 4.5 .NET pc (example: Windows 7 and Windows 8.1), without complie 2 version for each .NET? Thank!

Edit:
When I copy the .NET 3.5 application in question to a Windows 8.1 pc, Windows refuse to run my app, and require I install .NET 3.5. So if only complie with .NET 3.5, it dont make app can run in .NET 4.5 pc.

NoName
  • 7,940
  • 13
  • 56
  • 108
  • 1
    .Net 4.5 runs just fine on Windows 7 - it just needs to be installed? – StuartLC Dec 31 '13 at 17:20
  • 6
    Compile the app in lower version i.e. 3.5, it will work in the upper version – addy2601 Dec 31 '13 at 17:22
  • 2
    Why not just build for 3.5 then? That will run on both with a single build. – Nick Gotch Dec 31 '13 at 17:22
  • 1
    Unless you need capability of 4.5 you can as well stay with 3.5, which can run as back as WinXP and runs quite well on Mono. – PTwr Dec 31 '13 at 17:22
  • 1
    @PTwr: I copy a .NET 3.5 app to a Windows 8.1 pc and it show dialog requires install .NET 3.5 – NoName Dec 31 '13 at 17:31
  • @TuenkTk: .NET have no backward compatibility, instead you simply install multiple versions of Framework at once. 4.5 runs only 4.5 apps, 3.5 takes care of 3.5 and so on. Once Framework version is installed, there are no further problems. Why 8.1 do not have 3.5 by default is mystery to me. – PTwr Dec 31 '13 at 17:34
  • As far as I was aware .NET is fully backwards compatible. That doesn't preclude you from having multiple versions of the framework installed...but if a client machine only has .NET 4.5 and your code is compiled with 3.5 as a target, it should run just fine on the client machine without anything additional installed. In theory. *shrug* In practice that may work a bit differently, but as Grant pointed out....NET is supposed to be fully backwards compatible with itself. – Nevyn Dec 31 '13 at 17:39
  • @Nevyn: Windows 8.1 (.NET 4.5) refuse to run my .NET 3.5 app. – NoName Dec 31 '13 at 17:41
  • @Grant: It can barely be called compatibility if you need to re-target project to other version. – PTwr Dec 31 '13 at 17:42
  • This link is targetted to .NET 4, but the basics do apply. Also, you should read Grant's link as well. http://stackoverflow.com/questions/2816914/backwards-compatibility-of-net-framework-4 Distilled...Backwards compatibility is not guaranteed, due to bug fixes and programming practices, however in terms of method calls, all methods which were available in previous versions of .NET are still supported in later versions, guaranteeing backwards compatibility in that regard...but this is not true of all available programming methods. – Nevyn Dec 31 '13 at 17:44
  • 3
    You need a .config file to tell .NET that it is okay to use CLR v4 even though your assemblies say that it should use CLR v2. Use ``. – Hans Passant Dec 31 '13 at 18:51
  • @PTwr sadly WPF is not supported by Mono. – kenny Dec 31 '13 at 19:28
  • @kenny: that I did not knew, thanks for info. – PTwr Dec 31 '13 at 21:54

2 Answers2

9

Build with .NET 3.5 and add these lines to App.config and it worked for me:

<configuration>
  <startup>
    <supportedRuntime version="v4.0" />
    <supportedRuntime version="v2.0.50727"/>
  </startup>
</configuration>
NoName
  • 7,940
  • 13
  • 56
  • 108
  • how can I run my application with adding these lines but without change .net version from project's properties? Is this possible ? or Is there any diffirent way to run .net 4.5.1 application on .net 3.5 version ? – tpbafk Dec 15 '17 at 12:44
0

have your project target 3.5. then add another project to the solution that targets 4.5 and add links to the files from the first project.

Z .
  • 12,657
  • 1
  • 31
  • 56