1

I have created a Windows form project and targeted .NET 4.5

enter image description here

When I run the exe file on a machine that does not have .NET 4.0 installed, I get the following error message:

enter image description here

When I install .Net 4.0 and run the exe again I get the following error message:

enter image description here

Why the first message is not stating that the required .NET version is 4.5??

Ala
  • 1,505
  • 1
  • 20
  • 36
  • From the download website: This version of the .NET Framework runs side-by-side with the .NET Framework 3.5 and earlier versions, but performs an in-place update for the .NET Framework 4. – DonBoitnott Oct 02 '17 at 12:45
  • How you deploy your application? using setup project (visual studio setup project) OR installshield OR what else? it looks you have selected 4.0 in setup prerequisite and your application is built on 4.5? – Munawar Oct 02 '17 at 12:47
  • @Munawar I do not deploy my project. The exe file runs direct on the machine. – Ala Oct 02 '17 at 13:07

1 Answers1

1

According to this article, the version of the .NET Framework that an application runs on is determined in the following order:

  1. Configuration file (.config)
  2. Compiled version
  3. Latest version installed

By default if you set the target framework in Visual Studio you have a .config file that you deploy alongside your .exe. In this .config file Visual Studio creates an element <supportedRuntime> which has two attributes: version and sku

The default element for .NET 4.5 looks like this:

<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />

version is the version of the supported CLR - but that's not necessarily the same as the version of the .NET Framework because all of the .NET Frameworks from 4.0 to 4.7 are using the CLR 4.0.
Only sku (stock-keeping unit) specifies the exact release of the .NET Framework that your application supports.

According to this article, the sku attribute (containing a version number) is only being recognized starting with the .NET Framework 4.0.

As a side note: .NET 3.5 has used the sku as well but only to specify that you are supporting the .NET Framework Client Profile (sku="client") which doesn't exist any longer since .NET 4.5.

So the conclusion is:
You are getting the first error message because the CLR 2.0 loader in the .NET Framework 3.5 doesn't know anything about the sku attribute. It only knows that you are requesting a .NET 4.0 CLR. (If you don't have a .config file the required version of the CLR is compiled into the manifest of your .exe, which in your case is v4.0.30319.)

Only after installing the .NET Framework 4.0 the CLR 4.0 loader now reads the sku attribute and therefore knows that you also have to install the .NET Framework 4.5. That's why you're getting this two error messages.

haindl
  • 3,111
  • 2
  • 25
  • 31
  • @Ala There are a [few workarounds](https://learn.microsoft.com/en-us/dotnet/framework/deployment/initialization-errors-managing-the-user-experience) to a certain extent. But ultimately it would be a lot easier to just have some kind of Setup Bootstrapper that [checks](https://learn.microsoft.com/en-us/dotnet/framework/migration-guide/how-to-determine-which-versions-are-installed) and installs the required .NET Framework. There are several of these available, or you can build your own little installer e.g. in C++. – haindl Oct 03 '17 at 08:12
  • @Ala Basically these are mostly programs that are written without using .NET or using a version of .NET that is guaranteed to be installed on the client. But be aware that in newer OSes like Windows 10 (or Windows Server) you can install certain versions of .NET only as a system component or as an [OS update](https://learn.microsoft.com/en-us/dotnet/framework/install/on-windows-10) and not via a redistributable. – haindl Oct 03 '17 at 08:12