2

How can I force my C# application to run using the .net 4.5 runtimes? Even if I have 4.5, 4.6, 4.7, 4.8, etc also installed?

The reason I'm trying to do this is because I wanted to reproduce a bug that existed in an earlier version of WPF, so I wanted to run the code against an earlier version of .Net without having to uninstall anything.

I tried the following:

  • Target framework .Net 4.5 in the Application build settings

and then added the following config :

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

To test if it was running the 4.5 runtime, I tried to access something added after 4.5 :

private static bool Is462Installed()
{
    // API changes in 4.6.2: https://github.com/Microsoft/dotnet/blob/master/releases/net462/dotnet462-api-changes.md
    return Type.GetType("System.Security.Cryptography.AesCng, System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", false) != null;
}

But it returned true. So it is not using a .Net 4.5 runtime?

So I am guessing it will always use the latest runtimes that you have installed?

wforl
  • 91
  • 9
  • Not sure it is possible. .NET 4.6 is an in-place upgrade so it literally overwrites your 4.5 DLLs. Not sure about the others but seems likely. See also [this answer](https://stackoverflow.com/a/32699929/2791540) – John Wu Apr 07 '20 at 16:46
  • 1
    As the designers came fresh out of the dll Hell: https://en.wikipedia.org/wiki/DLL_Hell they realy wanted you to never have to worry about the precise version. Why exactly do you think you need to check for that version? It does sound like you got a serious case of XY problem. – Christopher Apr 07 '20 at 16:47
  • 1
    @Christopher I wanted to reproduce a bug that existed in an earlier version of WPF, so wanted to run code against an earliar version of .Net without having to uninstall anything. – wforl Apr 07 '20 at 17:00
  • 2
    @wforl That is indeed a valid case. Unfortunately I am drawing a blank on how to pull it off. – Christopher Apr 07 '20 at 17:06
  • It's a good question indeed. After a bit of research, this doesn't seem to be promising though. I guess your best bet would be running a VM for these kinds of tests. – 41686d6564 stands w. Palestine Apr 07 '20 at 17:28
  • Did you see this one? [assemblyBinding](https://stackoverflow.com/a/2046123/7894673) – Fateme Mirjalili Apr 07 '20 at 19:34

0 Answers0