Post

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
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
 ''' <A href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetdep/html/dotnetfxref.asp">http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetdep/html/dotnetfxref.asp</A>
 ''' 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
 ''' <A href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetdep/html/redistdeploy1_1.asp">http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetdep/html/redistdeploy1_1.asp</A>
 ''' 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
 ''' <A href="http://blogs.msdn.com/astebner/archive/2004/09/14/229802.aspx">http://blogs.msdn.com/astebner/archive/2004/09/14/229802.aspx</A>
 ''' 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
 ''' <A href="http://blogs.msdn.com/astebner/archive/2004/09/14/229574.aspx">http://blogs.msdn.com/astebner/archive/2004/09/14/229574.aspx</A>
 ''' 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 weird cut-n-paste issue and <comment> becoming hidden.

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