23

Using Mathematica 8, I can check the version of the .NET framework used by NETLink as follows:

Needs["NETLink`"]
InstallNet[];
LoadNETType["System.Environment"];
System`Environment`Version@ToString[]

"2.0.50727.5448"

As you can see, it is the old 2.0 framework. However, my machine has version 4.0 of the .NET framework installed. Is there a way to configure Mathematica to use the newer version?

WReach
  • 68,832
  • 4
  • 164
  • 269

1 Answers1

28

UPDATE The following steps are no longer necessary if one is using Mathematica version 9 -- it comes preconfigured to use .NET 4.x.

NETLink uses an interlude .NET application to broker communication with the framework. The application is called InstallableNET.exe (InstallableNET32.exe on 32-bit systems) and can be found in this directory:

SystemOpen @
  FileNameJoin[{$InstallationDirectory, "SystemFiles", "Links", "NETLink"}]

The application will use the version of the .NET framework that is configured in its .config file. To make it use version 4.0, add a new line within the startup element:

<supportedRuntime version = "v4.0"/>

Versions are tried in the order that they appear in the file, so place the preferred version first. The updated InstallableNET.exe.config will look something like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <!-- The supportedRuntime lines control which version of the .NET Framework will
         be used when .NET/Link is launched with InstallNET[].
         .NET/Link requires at least .NET 2.0. If you have .NET 3.0 installed,
         it will be used (note that the 3.0 version is really just the 2.0
         version with some extra assemblies).
      -->
      <supportedRuntime version="v4.0" />
      <supportedRuntime version="v2.0.50727" />
  </startup>
</configuration>

Note: If you're using Mono instead of Microsoft's CLR, you'll have to delete the first few (invisible) bytes from the XML file due to a Mono bug. One way to do this would be to tell your text editor to save the XML file as ASCII instead of Unicode. Another way would be to copy and paste the body of the file into a new file and then copy the new file over the previous InstallableNET.exe.config file.


UPDATE: .NET Versions 4.5.x and Later

The steps reported above will automatically select the latest installed version of the .NET Framework in the 4.x line. This includes the 4.5.x releases.

Beware, however, that the call to System`Environment`Version shown above can be misleading. All of the 4.5.x versions report a version string that starts with "4.0". For example, at time of writing my machine has version 4.5.2 installed. Yet:

System`Environment`Version@ToString[]
(* 4.0.30319.34209 *)

The magic number 34209 on the end of this version string actually identifies it as a 4.5.2 build - see StackOverflow answer (12972517) for a list of version strings.

The .NET documentation for Environment.Version expressly discourages its use to identify the 4.x minor version. Instead, it recommends querying the registry to identify the exact version:

Needs["Developer`"]
ReadRegistryKeyValues @
  "HKEY_LOCAL_MACHINE\\Software\\Microsoft\\NET Framework Setup\\NDP\\v4\\Full"

(* { Version->4.5.51209,
     TargetVersion->4.0.0,
     Install->1,
     MSI->1,
     Servicing->0,
     InstallPath->C:\Windows\Microsoft.NET\Framework64\v4.0.30319\,
     Release->379893
   } *)

The Release magic number is described in a Microsoft library article. But be aware that this registry key does not exist for earlier .NET versions.

If we want even more evidence to convince ourselves that Mathematica is accessing the latest .NET version, we can always attempt to access API that does not exist in earlier versions. Here is an example using a type that does not exist prior to version 4.5.x:

LoadNETType["System.Reflection.ReflectionContext"]
(* NETType[System.Reflection.ReflectionContext,6] *)
WReach
  • 68,832
  • 4
  • 164
  • 269
  • Interestingly, having trouble getting this to work with Mono 2.10.8, despite that it's supposed to. Will report back in once I've got this fixed up. – sblom Feb 18 '12 at 18:45
  • @sblom I haven't used Mono, but I have a guess anyway: perhaps Mono does not yet support the new partial version matching feature that was introduced in MS.NET v4 -- does a complete version specification work in place of the "v4.0"? – WReach Feb 18 '12 at 18:51
  • Nah--it supports it since Mono 2.10. It seems to be ignoring this one specific .exe.config file for some reason, though. I did find another workaround. It involved editing $InstallationDirectory/SystemFiles/Links/NETLink/Kernel/InstallNET.m and adding --runtime=v4.0 part to the line that reads netlinkPath = monoPath <> " --runtime=v4.0" <> " \"" <> netlinkPath <> "\"". I'll add another comment once I figure out how to get it to notice the .exe.config file. – sblom Feb 18 '12 at 20:39
  • 7
    Okay--got to the bottom of this. For some reason, Mono isn't reading an InstallableNET.exe.config that begins with a Unicode BOM. Once I deleted the BOM (which is redundant with the encoding="utf-8" part anyway), Mono began matching Microsoft's behavior here. – sblom Feb 20 '12 at 21:23
  • 1
    Great answer! I was able to load .NET 4.0.30319.269 from Mathematica 8 – Tomislav Topic Aug 19 '12 at 12:59
  • @WReach is update procedure in the documentation anywhere? I've never come across this info, and was too lazy to go pestering customer support. – telefunkenvf14 Dec 08 '12 at 05:45
  • @telefunkenvf14 I am not aware of any WRI documentation on this topic. I got this information from the relevant Microsoft .NET documentation. Note that these steps are not necessary for Mathematica version 9 as it comes preconfigured this way. – WReach Dec 08 '12 at 16:06
  • 3
    @sblom Thanks, this is extremely useful info about the BOM problem. I've just committed the fix to InstallableNET.exe.config, so this won't be an issue for future releases of Mathematica. – Todd Gayley Dec 10 '12 at 21:49
  • I've tried updating the .NET framework to v4.5 in Mathematica 10 following this approach but it still says I have version 4. I know I have versions 4.5 through 4.6 installed on my system. Can you suggest a workaround? – RunnyKine Jul 02 '15 at 00:15
  • @RunnyKine Despite appearances to the contrary, there is a good chance that you are getting access to the latest 4.x version. See the new 4.5.x section that I have added at the bottom of the response. – WReach Jul 02 '15 at 05:12
  • Thanks a lot for the addition. Good to know Mathematica is using the latest version of the .NET framework. – RunnyKine Jul 02 '15 at 05:23