Posts Determine if and what version of the .NET Framework is installed (including service pack levels)?
Post
Cancel

Determine if and what version of the .NET Framework is installed (including service pack levels)?

I came across an excellent example that shows how to determine what editions and service pack level of the existing Microsoft .NET Framework is installed.  Unfortunately for most of my readers, this code was in C++.  I've converted the code to VB.NET for everyone to benefit ;-)

Here's the code:

Option Explicit On

Option Strict On

 

Imports Microsoft.Win32

 

Module Module1

 

  Private Declare Auto Function GetSystemMetrics Lib "user32" (ByVal value As Integer) As Integer

 

  Private Const SM_TABLETPC As Integer = 86

  Private Const SM_MEDIACENTER As Integer = 87

 

  ' Constants that represent registry key names and value names to use for detection

  Private Const NET_FX_10_REG_KEY_NAME As String = "Software\Microsoft\.NETFramework\Policy\v1.0"

  Private Const NET_FX_10_REG_KEY_VALUE As String = "3705"

  Private Const NET_FX_10_SPx_MSI_REG_KEY_NAME As String = _

    "Software\Microsoft\Active Setup\Installed Components\{78705f0d-e8db-4b2d-8193-982bdda15ecd}"

  Private Const NET_FX_10_SPx_OCM_REG_KEY_NAME As String = _

    "Software\Microsoft\Active Setup\Installed Components\{FDC11A6F-17D1-48f9-9EA3-9051954BAA24}"

  Private Const NET_FX_10_SPx_REG_KEY_VALUE As String = "Version"

  Private Const NET_FX_11_REG_KEY_NAME As String = _

    "Software\Microsoft\NET Framework Setup\NDP\v1.1.4322"

  Private Const NET_FX_11_SPx_REG_KEY_NAME As String = _

    "Software\Microsoft\NET Framework Setup\NDP\v1.1.4322"

  Private Const NET_FX_11_REG_KEY_VALUE As String = "Install"

  Private Const NET_FX_11_SPx_REG_KEY_VALUE As String = "SP"

 

  Sub Main()

 

    Dim message As String

 

    Dim netfx10SPLevel As Integer = -1

    Dim netfx11SPLevel As Integer = -1

 

    ' Determine whether or not the .NET Framework 1.0 or 1.1 are installed

    Dim netfx10Installed As Boolean = IsNetfx10Installed()

    Dim netfx11Installed As Boolean = IsNetfx11Installed()

 

    ' If .NET Framework 1.0 is installed, get the service pack level

    Console.WriteLine(".NET Framework 1.0")

    Console.WriteLine("------------------")

    If (netfx10Installed) Then

 

      netfx10SPLevel = GetNetfx10SPLevel()

 

      If (netfx10SPLevel > 0) Then

        message = String.Format(".NET Framework 1.0 SP{0} is installed.", netfx10SPLevel)

      Else

        message = ".NET Framework 1.0 is installed with no service packs."

      End If

 

      Console.WriteLine(message)

 

    Else

      Console.WriteLine(".NET Framework 1.0 is not installed.")

    End If

 

    Console.WriteLine()

 

    ' If .NET Framework 1.1 is installed, get the service pack level

    Console.WriteLine(".NET Framework 1.1")

    Console.WriteLine("------------------")

    If netfx11Installed Then

 

      netfx11SPLevel = GetNetfx11SPLevel()

 

      If netfx11SPLevel > 0 Then

        message = String.Format(".NET Framework 1.1 SP{0} is installed.", netfx11SPLevel)

      Else

        message = ".NET Framework 1.1 is installed with no service packs."

      End If

 

      Console.WriteLine(message)

 

    Else

      Console.WriteLine(".NET Framework 1.1 is not installed.")

    End If

    Console.WriteLine()

 

    Console.WriteLine("Press [enter] to continue.")

 

    Console.Read()

 

    Environment.ExitCode = 0

 

  End Sub

 

  ''' <COMMENT>

  ''' Description: Uses the detection method recommended at

  ''' http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetdep/html/dotnetfxref.asp

  ''' to determine whether the .NET Framework 1.0 is installed on the machine

  ''' Inputs: None

  ''' Results: True if the .NET Framework 1.0 is installed, False otherwise

  ''' </COMMENT>

  Function IsNetfx10Installed() As Boolean

 

    Dim key As RegistryKey = Registry.LocalMachine.OpenSubKey(NET_FX_10_REG_KEY_NAME, False)

    Dim value As String = CType(key.GetValue(NET_FX_10_REG_KEY_VALUE, ""), String)

    key.Close()

 

    Return (value.Length > 0)

 

  End Function

 

  ''' <COMMENT>

  ''' Description: Uses the detection method recommended at

  ''' http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetdep/html/redistdeploy1_1.asp

  ''' to determine whether the .NET Framework 1.1 is installed on the machine

  ''' Inputs: None

  ''' Results: True if the .NET Framework 1.1 is installed, False otherwise

  ''' </COMMENT>

  Function IsNetfx11Installed() As Boolean

 

    Dim key As RegistryKey = Registry.LocalMachine.OpenSubKey(NET_FX_11_REG_KEY_NAME, False)

    Dim value As Integer = CType(key.GetValue(NET_FX_11_REG_KEY_VALUE, 0), Integer)

    key.Close()

 

    Return (value = 1)

 

  End Function

 

  ''' <COMMENT>

  ''' Description: Uses the detection method recommended at

  ''' http://blogs.msdn.com/astebner/archive/2004/09/14/229802.aspx

  ''' to determine what service pack for the .NET Framework 1.0 is installed on the machine

  ''' Inputs: None

  ''' Results: Integer representing SP level for .NET Framework 1.0

  ''' </COMMENT>

  Function GetNetfx10SPLevel() As Integer

 

    Dim pszSPLevel As String = Nothing

    Dim result As Integer = -1

 

    ' Need to detect what OS we are running on so we know what registry key

    ' to use to look up the SP level.

    Dim key As RegistryKey

    If IsCurrentOSTabletMedCenter() Then

      key = Registry.LocalMachine.OpenSubKey(NET_FX_10_SPx_OCM_REG_KEY_NAME, False)

    Else

      key = Registry.LocalMachine.OpenSubKey(NET_FX_10_SPx_MSI_REG_KEY_NAME, False)

    End If

    Dim value As String = CType(key.GetValue(NET_FX_10_SPx_REG_KEY_VALUE, ""), String)

    key.Close()

 

    If value.Length > 0 Then

      ' This registry value should be of the format #,#,#####,# where the last # is the SP level.

      ' We are just interested in the #.

      Dim values() As String = Split(value, ",")

      If values.Length = 4 Then

        Return CInt(values(3))

      End If

    End If

 

    Return result

 

  End Function

 

  ''' <COMMENT>

  ''' Description: Uses the detection method recommended at

  ''' http://blogs.msdn.com/astebner/archive/2004/09/14/229574.aspx

  ''' to determine what service pack for the .NET Framework 1.1 is installed on the machine

  ''' Inputs: NONE()

  ''' Results: Integer representing SP level for .NET Framework 1.1

  ''' </COMMENT>

  Function GetNetfx11SPLevel() As Integer

 

    Dim key As RegistryKey = Registry.LocalMachine.OpenSubKey(NET_FX_11_SPx_REG_KEY_NAME, False)

    Dim value As Integer = CType(key.GetValue(NET_FX_11_SPx_REG_KEY_VALUE, -1), Integer)

    key.Close()

 

    Return value

 

  End Function

 

  Function IsCurrentOSTabletMedCenter() As Boolean

    ' Use GetSystemMetrics to detect if we are on a Tablet PC or Media Center OS 

    Return (GetSystemMetrics(SM_TABLETPC) <> 0 OrElse GetSystemMetrics(SM_MEDIACENTER) <> 0)

  End Function

 

End Module

 

To use the code, just create a new console application, select all the code in module1.vb, paste the above source over the default created code.  The code is pretty well commented, but if you have any questions, please let me know.

 

[update] - fixed the comments not showing due to wierd cut-n-paste issue and <comment> becoming hidden.

This post is licensed under CC BY 4.0 by the author.