VBScript来检查.NET 2.0安装 [英] vbscript to check if .net 2.0 is installed

查看:139
本文介绍了VBScript来检查.NET 2.0安装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以共享一个VBScript如果.NET 2.0被安装在机器上检查。

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

我没有在网络上搜索,并在大多数此类的检查.NET安装应用程序只是看为特定的注册表项从而忽略了一个事实的安装可能会损坏。

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.

基本上我期待的脚本,它试图创建一个.NET对象(这应该是肯定可创建 - 如System.Object的),如果它failes - .NET或者未安装或安装已损坏(因此没有比好无.NET安装的话)。

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 corrupted (thus no better than having no .NET installed at all).

推荐答案

该负责人的方法来检测是否安装了.NET Framework的特定版本是通过检查相应的注册表键的存在。在这种情况下,你要找的这个键:

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 \微软\ .NETFramework \策略\ 2.0

如果是REG_SZ值50727是present,那么你就知道了框架2.0版本安装。

If the REG_SZ value "50727" is present, then you know that version 2.0 of the Framework is installed.

那么,你是如何做到这一点在VBScript?这里有一个小脚本,做到了这一点:

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

如果你想这个检查集成到一个的现有的VBScript中,我建议你把它变成一个函数返回一个布尔值(而不是显示一个消息框)根据.NET框架是否不正确的版本安装。然后你可以从你的脚本中调用这个函数。 注意:请确保您打开错误处理回退(或至少回到更合适的样式)在函数结束时,如果你走这条路线!你不希望使用上的错误继续下一步,除非你明确后处理在code中的错误。

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!


编辑:如果你确信你想通过尝试实例化一个共同的框架对象来确定一个.NET安装的有效性,剧本很相似。 (事实上​​,它甚至不是做访问注册表稍微简单一些。)和以前一样,的CreateObject 被使用,但这次来实例化基类的对象 System.Object的


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

不过,这会的没有的告诉你哪些的版本的.NET Framework的安装。本次测试将传递的任何版本,包括1.1,2.0,4.0,未来的版本中,等你的问题似乎说明了2.0版的要求,如果是那样的话,你真的应该考虑使用第一个选项。

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.

我的经验是,框架的这种破坏安装是极为罕见的,如果你看到他们经常因为我带领相信,你可以考虑只安装框架的正确版本作为水到渠成。我不相信能够实例化类型的对象 System.Object的真的更多的框架安装的有效性的一个真实的测试不是检查的注册表项或目录presence。

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.

这已经过测试工作,一个干净的Windows XP虚拟机的上没有的安装.NET Framework。它正确地报告失败。在其它机器上安装了.NET框架,它正确地报告成功。

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.

这篇关于VBScript来检查.NET 2.0安装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆