NSIS - .NET Framework version checking

I have recently become an avid fan of NSIS, the Nullsoft Installer Script. It is an extremely powerful way of creating installers, as it gives you control over almost every aspect the installer. Coming from a PHP/C#  background, I have also found it fun learning the quirky ways of using NSIS and am still not sure that I am doing things quite right.

I am starting to replace all of my .NET MSI installers with NSIS scripts.  You might ask why, and if there is enough interest it may be the subject of another post, however this post is primarily about a new header file I created: DotNetVer.nsh.

As I was replacing the MSI, I needed a reliable way to check for the .NET framework.  Also, as I have a number of different projects, I have to check for different .NET framework versions.  I did a quick search in the NSIS wiki and found a few ways of checking for the framework.  However they seemed to test different things, and each script was focused towards a specific version of the framework.

The other thing about these scripts was the way they were used; call a funtion or macro and then test a variable. I much prefered the simplicity of WinVer, LogicLib extension:

${If} ${IsWinXP}

it just felt so much cleaner to me.

Thus, using WinVer as my inspiration, I went about creating the DotNetVer LogicLib extension.

First of all I had to clarify, for myself, what was the standard way of testing for each framework version and service pack version. After a quick googling, I found a couple of Microsoft blog posts: here, here and here.

To test for each version of the framework (including service pack), I would enumerate through the following registry key (each sub-key is a version of the .NET framework):

HKLM\SOFTWARE\Microsoft\NET Framework Setup\NDP

And testing\capturing the following registry values:

Install SP

If the value of 'Install' is anything other than 0, that means that version of the framework has been installed.
The value of SP is the version of the service pack installed for that framework version.

Installing DotNetVer.nsh

Simply download the DotNetVer archive file, uncompress and copy into the NSIS include folder.  This is usually "[Program Files]\NSIS\Include\".

Using DotNetVer.nsh

Firstly, make sure you 'include' DotNetVer and LogicLib at the start of your installation script.

!include LogicLib.nsh !include DotNetVer.nsh

"HasDotNet<version>" checks if the current computer (on which the installer is running) has the specific version of the .NET framework installed.

Example:

${If} ${HasDotNet2.0} DetailPrint &quot;.NET Framework 2.0 installed&quot; ${EndIf}

<version> can only be replaced with the following values:
   1.0
   1.1
   2.0
   3.0
   3.5

"AtLeastDotNetServicePack" checks if the .NET framework has a service pack version at least as specified.
"IsDotNetServicePack" checks if the .NET framework has a service pack version exactly as specified.
"AtMostDotNetServicePack" checks if the .NET framework has a service pack version at most as specified.

Example:

${If} ${HasDotNet2.0} ${If} ${DOTNETVER_2_0} AtLeastDotNetServicePack 1 DetailPrint &quot;Microsoft .NET Framework 2.0 SP1 installed. No update necessary&quot; ${Else} DetailPrint &quot;Microsoft .NET Framework 2.0 SP1 not installed. Please install before trying install again&quot; ${EndIf} ${EndIf}

I hope that people find this useful.

Trackback URL for this post:

http://ontheperiphery.veraida.com/trackback/38