Welcome to MSDN Blogs Sign in | Join | Help

Sample code to detect .NET Framework install state and service pack level

Introduction

Many installers and applications require that one or more versions of the .NET Framework be installed on the system in order to be able to install and function correctly.  This article provides sample C++ code that can be used in a setup program or in an application's code to detect the install state and service pack level of various versions of the .NET Framework.

Note - when used in a setup program, this sample code is intended to be used in a setup EXE bootstrapper.  It is not intended for use within an MSI.  If your setup program is a single MSI, you can use Windows Installer AppSearch and RegLocator tables to detect the install state.  If you use WiX to create your MSI, you can use the information provided in the WixNetFxExtension instead of implementing this logic yourself.  Refer to How To: Check for .NET Framework Versions in the WiX documentation for more details.

.NET Framework versions that can be detected by the sample code

The sample code available via this article supports detecting the install state and service pack level for the following versions of the .NET Framework:

  • .NET Framework 1.0
  • .NET Framework 1.1
  • .NET Framework 2.0
  • .NET Framework 3.0
  • .NET Framework 3.5
  • .NET Framework 4 (Client and Full)

Registry-based detection code

The simple version of the sample code queries the officially documented registry values that are intended to be used to detect the presence of each version of the .NET Framework.

This sample code can be downloaded from the following location:

Registry-based detection code and more in-depth checking that loads mscoree.dll

The more thorough version of the sample code queries the officially documented registry values like in the previous sample.  In addition, it performs an additional check that loads mscoree.dll and uses some of its APIs to query for the presence of specific versions of the .NET Framework.  The algorithm to perform the additional check was originally introduced in this blog post.

This sample code can be downloaded from the following location:

Creating a Visual Studio project to compile the sample code

The sample code available for download via the links above is in the form of a single C++ source (.cpp) file.  If you are having trouble getting this code to compile on your system, you can refer to the instructions in this blog post to create a Visual C++ project that can be used to compile the sample code into a sample executable file.

 

Published Tuesday, June 16, 2009 4:18 PM by astebner

Comments

Tuesday, November 24, 2009 9:38 AM by astebner

# re: Sample code to detect .NET Framework install state and service pack level

For anyone reading this in the future, here is a link to a starting point you can use to convert this sample code to C# - http://geekswithblogs.net/lorint/archive/2006/01/30/67654.aspx.

One note of caution here - if you write this detection code in C#, then if the computer you run it on doesn't have any version of the .NET Framework installed on it, the code won't be able to run.  If you know for sure that at least one version of the .NET Framework will always be present on the computers that you run it on, it should probably be OK to write the detection code in C# though.

Also, if you need to detect the .NET Framework version or service pack level within an MSI, you should not use this detection code or a custom action.  You can instead use the AppSearch, RegLocator and LaunchCondition tables.  There are built-in detection properties in WiX that you can use for this if you use WiX to create your MSI.  You can find more information about these detection properties at http://wix.sourceforge.net/manual-wix3/wixnetfxextension.htm.

Anonymous comments are disabled
 
Page view tracker