Build a .NET application that runs on any version of .NET, if only the basic requirements

I am using VS2017 for Windows 10 to work on a C # project. This is a really small console application that does nothing but requires administrator privileges and then launches HTA.

The code only refers to these .NET assemblies:

using System; using System.Diagnostics; using System.IO; 

Since they all exist in .NET from 2.0 onwards, it seems that it is possible to create an application without specifying anything but .NET 2.0. But at the same time, the application displays the message "you must install .NET 2.0 or 3.5" when it starts on a system with only .NET 4.0 or higher.

Is there any way to create this application so that it does not indicate a .NET version? I tried to remove <TargetFrameworkVersion> from the .csproj file, but it is still building, thinking it is a .NET 4.0 application.

UPDATE

Based on the proposed answer and this article, I updated App.config to show that it supports both v2 and v4:

 <startup useLegacyV2RuntimeActivationPolicy="true"> <supportedRuntime version="v2.0.50727"/> <supportedRuntime version="v4.0"/> </startup> 

Then I built with the target installation the NET Framework 4.6.1 and ran it on the Win7Pro-64bit system with only the .NET Framework 2.0 / 3.5 installed. It appeared the message "First you must install one of the following versions of the .NET Framework" and only in the list v4.0.30319.

Then I built with the target installation the NET Framework 2.0 and launched it on the Win10Pro-64bit system with only the .NET Framework 4.0 installed. It displayed a message that I should have "NET Framework 3.5", or my application may not work correctly. I tried the "skip this" option and the application did not work.

Starting the system in accordance with the intended purpose, the application works fine.

Why won't he use any framework as he is designated as supporting both ?

+5
source share
1 answer

Inside App.Config, you can specify a version of the runtime that you can support.

https://docs.microsoft.com/en-us/dotnet/framework/migration-guide/how-to-configure-an-app-to-support-net-framework-4-or-4-5

 <configuration> <startup> <supportedRuntime version="v1.1.4322"/> <supportedRuntime version="v2.0.50727"/> <supportedRuntime version="v4.0"/> </startup> </configuration> 

As with everything, you should verify that your application can actually work in all of the specified versions.

+2
source

Source: https://habr.com/ru/post/1273944/


All Articles