Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a .NET app that works on any .NET version if only basic requirements

Tags:

c#

.net

I'm using VS2017 on Windows 10 to work on a C# project. It is a really small console app that does nothing but require administrator privileges and then runs an HTA.

The code references only these .NET assemblies:

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

Since all of these exist in .NET since 2.0 and forward, it seems it should be possible to create the app without having to specify anything except .NET 2.0. But doing this causes the app to display the "must install .NET 2.0 or 3.5" message when running it on a system which has only .NET 4.0 or greater.

Is there a way to create this app so it doesn't specify a .NET version? I tried deleting the <TargetFrameworkVersion> from the .csproj file but it still builds thinking it is a .NET 4.0 app.

UPDATE

Based upon a suggested answer and this article, I updated the App.config to show it supports both v2 and v4:

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

I then built with target set to NET Framework 4.6.1 and ran it on a Win7Pro-64bit system with only NET Framework 2.0/3.5 installed. It displayed the message "You must first install one of the following versions of .NET Framework" and only listed v4.0.30319.

I then built with target set to NET Framework 2.0 and ran it on a Win10Pro-64bit system with only NET Framework 4.0 installed. It displayed a message that I had to "NET Framework 3.5" or my app may not work correctly. I tried the "skip this" option and the app didn't work.

Running on the system according to the target, the app works fine.

Why won't it just use whichever framework is available since it is designated as supporting both?

like image 775
AdvApp Avatar asked Dec 08 '17 20:12

AdvApp


1 Answers

Inside the App.Config you can specify the version of the runtime 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 anything, you should test that your application actually can run on all of the specified versions.

like image 106
mageos Avatar answered Oct 02 '22 20:10

mageos