Vbscript to check if .net 2.0 is installed

you can share vbscript that checks if .NET 2.0 is installed on the machine.

I did a search on the Internet and most of this “checking if .net is installed”, applications simply look for specific registry keys, while ignoring the fact that the installation may be corrupted.

I am mainly looking for a script that tries to create a .NET object (which, of course, must be created - for example, System.Object), and if it fails - .NET is either not installed or the installation is corrupted (there is no better way than do not install .NET at all).

+3
source share
3 answers

The official way to determine if a particular version of the .NET Framework is installed is to check for the presence of an appropriate registry key. In this case, you are looking for this key:

HKLM\SOFTWARE\Microsoft\.NETFramework\Policy\v2.0

If REG_SZ value "50727" is present, 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 want to integrate this check into existing VBScript, I suggest you turn it into a function that returns a Boolean value (instead of displaying a message box) depending on whether the correct version was installed by the .NET Framework. Then you can call this function from your script. Note. Make sure you return error handling (or at least back to a more appropriate style) at the end of the function if you go this route! You do not want to use On Error Resume Next unless you explicitly handle errors later in your code.

 On Error Goto 0 ''#Turn "On Error Resume Next" back off! 

EDIT: If you are sure you want to determine if the .NET installation is correct by trying to instantiate a common frame object, the script is very similar. (Actually, this is even a little easier than accessing the registry.) As before, CreateObject used, but this time to create 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 versions 1.1, 2.0, 4.0, future versions, etc. Your question allegedly states a requirement for version 2.0, in which case you really should consider using the first option.

My experience is that such “damaged” Framework installations are extremely rare, and if you see them as often as it seems to me, you can simply install the correct version of the Framework for granted. I am not convinced that the possibility of creating an instance of an object of type System.Object really no longer a “true” test of the validity of the Framework installation, than a check for the presence of registry keys or directories.

This is now tested to work on a clean Windows XP virtual machine without the .NET Framework installed. It correctly reports an error. On other computers with the .NET Framework installed, it correctly reports success.

+2
source

It also works and is an exact copy of the MSDN website with recommended methods for testing the .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 
+2
source

The best way to find out if .NET FRAMEWORK 2 is installed, I would recommend making scripts that look for "2 THINGS";

  • Make sure RegKey still exists, for example, Cody Gray.

  • I would write code that checks if NET FRAMEWORK 2 (located in the Windows directory) is less than, for example, 75 MB (usually around 82-87 MB), if so, that it is either deleted or damaged.

combining these 2 codes, you can find out if NET FRAMEWORK 2 is installed correctly on the user's computer or not.

0
source

Source: https://habr.com/ru/post/901987/


All Articles