vbscript 检查是否安装了.net 2.0

发布于 2024-10-06 14:21:10 字数 228 浏览 4 评论 0原文

你能分享一个检查计算机上是否安装了.NET 2.0的vb脚本吗?

我在网上进行了搜索,大多数此类“检查 .net 是否已安装”应用程序只是查找特定的注册表项,从而忽略了安装可能已损坏的事实。

基本上我正在寻找尝试创建 .NET 对象的脚本(这应该是肯定可以创建的 - 例如 System.Object),如果失败 - .NET 要么未安装,要么安装已损坏(因此并不比没有 .NET 更好)。 NET 已安装)。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只是我以为 2024-10-13 14:21:10

检测是否安装了特定版本的 .NET Framework 的官方方法是检查相应的注册表项是否存在。在这种情况下,您正在寻找这个密钥:

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

如果 REG_SZ 值“50727”存在,则您知道已安装 Framework 2.0 版本。

那么,如何在 VBScript 中做到这一点呢?这里有一个小脚本可以做到这一点:

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 Framework。然后您可以从脚本中调用此函数。 注意:如果您选择此路线,请确保在函数结束时关闭错误处理(或至少返回到更合适的样式)!您不希望使用On Error Resume Next,除非您稍后在代码中显式处理错误。

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

编辑:如果您确信要通过尝试实例化通用框架对象来确定 .NET 安装的有效性,则该脚本非常相似。 (事实上​​,它甚至比访问注册表还要简单一点。)和之前一样,使用了CreateObject,但这次是实例化基类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 的要求,如果是这种情况,您确实应该考虑使用第一个选项。

我的经验是,这种“损坏”的框架安装极其罕见,如果您像我相信的那样频繁地看到它们,那么您可能会考虑安装正确版本的框架,这是理所当然的事情。我不相信能够实例化 System.Object 类型的对象实际上是对框架安装有效性的“真实”测试,而不是检查注册表项或目录是否存在。

现在已经过测试,可以在未安装 .NET Framework 的干净 Windows XP 虚拟机上运行。它正确报告故障。在安装了 .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\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.

断舍离 2024-10-13 14:21:10

这也有效,并且是 MSDN 网站上测试 .net 安装的推荐方法的精确副本。

网站 - 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 

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 
权谋诡计 2024-10-13 14:21:10

了解 .NET FRAMEWORK 2 是否正确安装的最佳选择是我建议创建一个脚本来查找“2 THINGS”;

  1. 检查 RegKey 是否仍然存在,如“Cody Gray”所示。

  2. 我会编写一段代码来检查 NET FRAMEWORK 2(位于 Windows 目录中)是否小于 75 MB(通常约为 82 -87 MB),如果小于,则表明它已被卸载或损坏。

通过将这两个代码放在一起,您将能够知道 NET FRAMEWORK 2 是否正确安装在用户计算机上

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文