Everything Web

NSIS - .NET Framework version checking

January 21, 2010dgrinberg

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

Comments (9)

Hello

Hello,I can't find "Util.nsh",Can you help me?Thank you .

Util.nsh

Doesn't it come with NSIS? Check under "C:\Program Files\NSIS\Include" Or download the latest http://nsis.sourceforge.net/Download

OK

OK,Thank you very much. I'm Chiness,Where are you from?

.NET 4.0 Support (Modified)

Updated version 1.2 now available, fixed .NET 4.0 detection across windows platforms...if you downloaded before, just re-download the new file below...

Hello, I've modified this to support .NET Framework 4.0. and updated the example and help info inside the script. Also added support to detect client or full profile installs of 4.0

You'll find it here http://download.remotehams.com/examples/DotNetVer.nsh

Enjoy, Brandon

Support for .Net 4.0?

Was just checking to see if .Net 4.0 would be getting support shortly...

Looked at code and 4.0 registry, MS made some changes

I was going to go ahead and update your include file to support 4.0 for my own purposes because I need to get this new installer ready by the end of the day. Was surprised to find that Microsoft completely changed the way they report the versioning of .Net 4 in the registry.
    This could potentially have repercussions for your existing code base, although I can't tell for 100% if it does. I notice in your code that you enum the number of versions listed in the NDP key. For some odd reason, .Net 4 insists on installing three new keys instead of just the one like all previous versions (CDF, v4, and v4.0).
      The v4 key appears to have the versioning values that you search for, but now there's two subkeys that must be checked (Client and Full). I'm guessing that you could technically get away with just checking the Client version, but I haven't studied up enough on version 4.0 yet to know whether or not that's the new baseline framework, or if it needs to be the full. Windows Update provides only the client, but the redistributable download is the full.
        Here's the new layout as I'm viewing my HKLM\SW\MS\NET Framework Setup\NDP key:
        • \CDF
        • \CDF\v4.0
        • \v2.0.50727 (contains Install and SP DWORDS)
        • \v3.0 (contains Install and SP DWORDS)
        • \v3.5 (contains Install and SP DWORDS)
        • \v4
        • \v4\Client (contains only Install DWORD at this time, but will likely have SP in a year or so)
        • \v4\Full (contains only Install DWORD at this time, but will likely have SP in a year or so)
        • \v4.0
        As it stands, to keep this project moving, I'm going to create a separate DotNetVer4+.nsh file that scans for versions 4 or newer and include it as well as yours. I think mine will just check for the client version. I know that at this point none of the systems I'm going to be installing on even have 3.5, so it's kinda a moot point to be doing this, but I'm fixated on it now.
          Would love to see how you ultimately address it in your code.
            And thanks for the original, by the way, it'll be really helpful for all of my future NSIS projects.

            DotNet 4.0

            I have not yet had a chance to look into DotNet 4.0 projects, hence I have not updated this header file. However, the changes do sound interesting - I will have to have a poke around. I hope you find this header useful!!

            Download problem: DotNetVer

            Hi: I'am just trying to download the DotNetVer archieve file but I can't. When I click on the link it always shows Access Denied. Can u help me? Thank you!

            Download fixed

            Appologies, a Drupal upgrade seemed to remove all files.

            It should be fixed now, please try again - If not I can email it to you if you still want it.

            > http://ontheperiphery.veraida.com/node/39/release