Welcome to AddressOf.com Sign in | Join | Help

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.

Published Tuesday, September 21, 2004 5:21 PM by CorySmith
Filed under: ,

Comments

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

Tuesday, September 21, 2004 8:00 PM by Christopher Lewis
How about:


Imports System
Imports Microsoft.Win32
Imports Microsoft.VisualBasic

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

Tuesday, September 21, 2004 8:20 PM by Cory Smith
If your doing this via a command line compile, then you probably would need it, however, since I'm using Visual Studio, the System and Microsoft.VisualBasic imports are automatic.

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

Monday, December 06, 2004 7:16 AM by Jochen Wezel
Here is an updated version which
1. doesn't throw exception if there is only 1.1 but not 1.0 installed
2. as a web page, not a console application


<%@ Import Namespace="Microsoft.Win32" %>
<script runat="server">

sub page_load()
Main
end sub

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"

class cConsole
sub WriteLine(text as string)
HttpContext.Current.Response.Write (text & "<br>")
end sub
sub WriteLine()
HttpContext.Current.Response.Write ("<br>")
end sub
sub ReadLine()
end sub
end class
dim Console as new cconsole

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.")



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
if not key is nothing then
value= CType(key.GetValue(NET_FX_10_REG_KEY_VALUE, ""), String)
key.Close()
end if



Return (value <> "")



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 String
if not key is nothing then
value= CType(key.GetValue(NET_FX_11_REG_KEY_VALUE, ""), String)
key.Close()
end if

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 <> "" 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


</script>

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

Thursday, December 16, 2004 11:22 AM by Peter
Has no-one still figured out a good way to determine that the framework is equal to or GREATER THAN the version you need - ie: if you need 1.1.4322 or greater and they have 1.2 installed - then you don't need to install 1.1.4322.

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

Saturday, February 12, 2005 12:14 PM by bernd
hello,
what exactly has to be done with
the vb code on
http://addressof.com/blog/articles/FrameworkVersionInfo.aspx
to determine the .net framework sp level to get it to run?
i tried to cut and paste it in a word makro vbs editor, but seems not to work if i start run.
bernd ! blackforest2000@hotmail.com

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

Tuesday, March 08, 2005 10:24 AM by jo
hi,
how should that run on a machine without 1.1
when compiled with 1.1 ?

still thinking

jo

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

Wednesday, July 13, 2005 3:25 AM by Ian Thomas
I think some of the later comments really want the Registry keys wrapped into a VBScript.

The comment about "a good way to determine that the framework is equal to or GREATER THAN the version you need" is a must-have for any installer code - eg, writing a Custom Action for the Windows Installer (MSI) with or without the aid of WiX.

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

Tuesday, November 22, 2005 5:26 AM by Kost
How should it run(VB.NET code) on the mahine that hasn't Net Framework installed at all

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

Wednesday, November 29, 2006 7:43 PM by Weaver

' kb/318785/en-us says we can get the NET Major from the directory names

Option Explicit On

Option Strict On

Imports System.IO

Public Class DotNetDetect

   Public Function GetNetVersion() As String

       ''''''''''''''''''''''''''''''''''''''''''''''''''''

       '   PURPORSE : Get the highest .NET framework version from the directy name

       '              support.microsoft.com/kb/318785/en-us

       '

       '              Assumes microsoft will continue naming convention

       '              %systemroot%\Microsoft.NET\Framework\vN.N.NXXXX

       '

       '   SECURITY : Authenticated Users (Win2000)

       '

       '    RETURNS : Numeric portion of direcory name as a [String]

       ''''''''''''''''''''''''''''''''''''''''''''''''''''

       Dim SystemDirectory As String = Environment.GetEnvironmentVariable("SystemRoot")

       Dim strWorkingDir As String = SystemDirectory & "\Microsoft.NET\Framework\"

       Dim NetFramework As Directory

       Dim strProcessDirs() As String

       Dim strVersion As String

       Dim intNetMajor As Double

       Dim intVal As Double

       Dim i As Integer

       Dim j As Integer = Len(strWorkingDir) + 1

       If System.IO.Directory.Exists(strWorkingDir) = True Then

           ReDim Preserve strProcessDirs(NetFramework.GetDirectories(strWorkingDir).GetUpperBound(0))

           ' get the subdirectories that match the filter

           NetFramework.GetDirectories(strWorkingDir, "v?.*").CopyTo(strProcessDirs, (0))

           '

           For i = 0 To strProcessDirs.GetUpperBound(0)

               If strProcessDirs(i) <> "" Then

                   ' strip the path and the 'v' from the directory name

                   strVersion = Microsoft.VisualBasic.Right(strProcessDirs(i), Len(strProcessDirs(i)) - j)

                   ' get the left three chars as a decimal

                   intVal = CType(Microsoft.VisualBasic.Left(strVersion, 3), Double)

                   'resolve the highest version number

                   If intVal > intNetMajor Then

                       intNetMajor = intVal

                       ' return the higer version as string

                       GetNetVersion = strVersion

                   End If

               End If

           Next

       End If

   End Function

End Class

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

Monday, July 02, 2007 2:26 AM by Jack
All the functions listed here are using the key variable under the assumption it is set. However, if the registry keys are missing, this variable is Nothing. Please update the functions with "If Not key Is Nothing Then" logic to ensure functionality if .NET is not installed.
Anonymous comments are disabled