开发者

vbscript to check if .net 2.0 is installed

can you share a vbscript which checks if .NET 2.0 is installed on a machine.

I did a search on the web and the most of the such "check if .net is installed" applications are just look up for a particular registry keys thus ignoring the fact the the installation could be corrupted.

Basically I am looking for script which tries to create a .NET object (which should be surely createable - e.g. System.Object) and if it failes - .NET either is not installed or the installation is corru开发者_高级运维pted (thus no better than having no .NET installed at all).


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.


This works as well and is an exact copy off of the MSDN website of recomended ways to test .net installation.

Website - http://support.microsoft.com/kb/318785/en-us

''official MSDN verison 2.0
value = oShell.RegRead("HKEY_LOCAL_MACHINE\Software\Microsoft\NET Framework Setup\NDP\v2.0.50727\Version")  
''#Catch the error 
If Err.Number = 0 Then     
    ''#Error code 0 indicates success        
    MsgBox("Version 2.0 of the official .NET Framework is installed.") 
Else     
''#Any other error code indicates failure     
    MsgBox("Version 2.0 of the official .NET Framework is NOT installed.") 
End If 


The best option to know if .NET FRAMEWORK 2 is installed properly I would recommend to make a script that look for "2 THINGS";

  1. Check if the RegKey is still there, like 'Cody Gray' showed.

  2. I would write a code that checks if NET FRAMEWORK 2 (located on the Windows directory) is smaller than for example 75 MB (which normally is around 82 -87 MB ), if so than its either uninstalled or damaged.

by putting these 2 codes together you'll be able to know if NET FRAMEWORK 2 is correctly installed on the users machine or not

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜