23
$\begingroup$

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?

$\endgroup$

1 Answer 1

28
$\begingroup$

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] *)
$\endgroup$
11
  • $\begingroup$ 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. $\endgroup$
    – sblom
    Feb 18, 2012 at 18:45
  • $\begingroup$ @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"? $\endgroup$
    – WReach
    Feb 18, 2012 at 18:51
  • 7
    $\begingroup$ 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. $\endgroup$
    – sblom
    Feb 20, 2012 at 21:23
  • 1
    $\begingroup$ Great answer! I was able to load .NET 4.0.30319.269 from Mathematica 8 $\endgroup$ Aug 19, 2012 at 12:59
  • 3
    $\begingroup$ @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. $\endgroup$ Dec 10, 2012 at 21:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.