The official way to detect if a particular version of the .NET Framework is installed is by checking for the existence of the corresponding registry key. In this case, you're looking for this key:
HKLM\SOFTWARE\Microsoft\.NETFramework\Policy\v2.0
If the REG_SZ value "50727" is present, then you know that version 2.0 of the Framework is installed.
So, how do you do this in VBScript? Here's a little script that does just that:
Option Explicit
Dim oShell
Dim value
''#If the key isn't there when we try to read it, an error will be generated
''# that we will later test for, so we want to automatically resume execution.
On Error Resume Next
''#Try reading the registry value
Set oShell = CreateObject("WScript.Shell")
value = oShell.RegRead("HKLM\SOFTWARE\Microsoft\.NETFramework\Policy\v2.0\50727")
''#Catch the error
If Err.Number = 0 Then
''#Error code 0 indicates success
MsgBox("Version 2.0 of the .NET Framework is installed.")
Else
''#Any other error code indicates failure
MsgBox("Version 2.0 of the .NET Framework is NOT installed.")
End If
If you're wanting to integrate this check into an existing VBScript, I suggest that you turn it into a function that returns a Boolean
value (instead of displaying a message box) depending on whether or not the proper version of the .NET Framework is installed. Then you can call this function from within your script. Note: Make sure that you turn the error handling back off (or at least back to a more appropriate style) at the end of the function if you go this route! You don't want to be using On Error Resume Next
unless you're explicitly handling the errors later in your code.
On Error Goto 0 ''#Turn "On Error Resume Next" back off!
EDIT: If you are convinced that you want to determine the validity of a .NET installation by trying to instantiate a common framework object, the script is very similar. (In fact, it's even a little simpler than doing registry access.) As before, CreateObject
is used, but this time to instantiate an object of the base class System.Object
:
On Error Resume Next
Dim testObj
Set testObj = CreateObject("System.Object")
If Err.Number = 0 Then
MsgBox("Success")
Else
MsgBox("Failure")
End If
However, this will not tell you which version of the .NET Framework is installed. This test will pass for any version, including 1.1, 2.0, 4.0, future versions, etc. Your question seemed to state a requirement for version 2.0, and if that's the case, you really should consider using the first option.
My experience has been that such "corrupted" installations of the Framework are extremely rare, and if you're seeing them as often as I'm led to believe, you might consider just installing the proper version of the Framework as a matter of course. I'm unconvinced that being able to instantiate an object of type System.Object
is really any more of a "true" test of the validity of the Framework installation than checking for the presence of Registry keys or directories.
This has now been tested to work on a clean Windows XP virtual machine without the .NET Framework installed. It correctly reports failure. On other machines with the .NET Framework installed, it correctly reports success.