Make same app compatible for Net3.5 & 4.0

2019-08-27 11:54发布

问题:

I have a set of applications as a single Project developed in C# using .NET3.5. The app doesn't work in PC with NET4, I added and testing to see all functionality is working in NET4 or not. Changed the Net framework to 4.0 instead of 3.5.

The same app got compiled & build without a single error in NET4. The app contains UserSettings in Properties.Settings. I changed the version in to 4.0.0 from 2.0.0 and now app starts its execution.

My QUESTION: The code is compatible for NET3.5 & 4.0. But the only problem lies at present is value of Version in in appConfig will be different for version with .NET3.5 & .NET4.0. Is their anyway, I can use the same code for NET3.5 & 4 in addition to handle the above mentioned situation i.e. value of Version tag ? Also can I add

  <startup>
<!-- FOR .NET 3.0 & 3.5, 3.0.50727 CLR is only used -->

<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
<supportedRuntime version="v2.0" />  <!-- For Net 2.0, 3.0, 3.5 -->

to make the app run on PC with NET 4.0 or 3.5 ?

Any help, guidance is appreciated.

Thanks

回答1:

You should build against v3.5 instead of v4. Then I'd expect you to be able to run it on machines with either just 4.0 or just 3.5 without any app.config file at all - or one which doesn't mention supported runtimes. It's expected that most code which runs on 3.5 should just be able to run on 4.0 without change.

(One change is around native code in mixed mode assemblies, IIRC - but unless you know that you're including native code, I doubt that you'll be bitten by that.)