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] *)