6

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?

AdvApp
  • 1,094
  • 1
  • 14
  • 27
  • 2
    Did you change the "Target framework" version on the drop-down of the "build" tab from the project's properties? You should be able to build it as a.NET 2.0 project, and run it on any computer with 2.0 or higher installed. – Bradley Uffner Dec 08 '17 at 20:05
  • nope. you have to be tied to a clr version. or you could generate native assemblies. – Daniel A. White Dec 08 '17 at 20:06
  • Though quite subjective, I think that kind of ugly experience is in fact possible, by forcing most developers to use .NET 4.x and say goodbye to .NET 2.x/3.x. Without such pains, many would stick to those legacy platforms and refuse to upgrade. Again, .NET Core introduces even more breaking changes. – Lex Li Dec 09 '17 at 02:12
  • What type of application is this? Is it a console application, Windows Forms or WPF? – mageos Dec 11 '17 at 22:05
  • @mageos - per the OP it is a very small console app. I can provide the project. – AdvApp Dec 12 '17 at 23:22

2 Answers2

3

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

https://learn.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.

mageos
  • 1,216
  • 7
  • 15
  • I updated my original post. I tried that and it didn't work even though this article (https://learn.microsoft.com/en-us/dotnet/framework/migration-guide/how-to-configure-an-app-to-support-net-framework-4-or-4-5) says that it should. – AdvApp Dec 08 '17 at 22:03
  • If anyone can supply me with a sample program that does this very thing -- runs on both .NET 2.0/3.5 and .NET 4+ I would be very appreciative. I still have not been able to get this to work as of VS2017 15.5.7. Or, I could provide my simple program and see if you can compile it and have it work for you. – AdvApp Mar 02 '18 at 22:48
  • Complete "Duh" moment. I had forgotten to copy over the .config when copying the EXE to the test system. – AdvApp Jul 06 '18 at 19:37
0

It's not app.config (literally), but rather myappname.exe.config e.g. C:\Program Files (x86)\MSBuild\14.0\Bin\csc.exe.config

Harun Diluka Heshan
  • 1,155
  • 2
  • 18
  • 30