使用VBScript启动.NET安装并等待completition

时间:2017-05-14 22:53:49

标签: .net vbscript

由于我必须从USB记忆棒运行我的.NET应用程序,我需要一种方法来检查是否安装了.NET。

我创建了这个VBScript(在Win10中),但它没有等待安装程序完成,我错了

Option Explicit
Dim oShell
Dim value

On Error resume next

Set oShell = CreateObject("WScript.Shell")
value = oShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\Release")

' check for .NET Framework 4.5.1 -> 378xxx

If ((Err.Number <> 0) Or (Clng(value) < 379000)) Then
    ' .NET 4.5.1 is not istalled....install it (and wait for completition)
    MsgBox("Install .NET")
    oShell.Run "NDP451-KB2858728-x86-x64-AllOS-ENU.exe", 1, True
End If

oShell.Exec("MyApp.exe")

1 个答案:

答案 0 :(得分:2)

如果您确实希望脚本等待.NET Framework安装程序完成,那么您的另一个选项应该是连续循环。

看看以下脚本:

Option Explicit
Dim oShell
Dim value
Dim PackageName

PackageName = "NDP451-KB2858728-x86-x64-AllOS-ENU.exe"

Function IsProcessRunning(StrProcess)
  Dim Process
  IsProcessRunning = False
  For Each Process in GetObject("winmgmts://.").InstancesOf("Win32_process")
    If UCase(Process.Name) = UCase(StrProcess) Then
    IsProcessRunning = True
    Exit Function
    End If
  Next
End Function

On Error Resume Next

Set oShell = CreateObject("WScript.Shell")
Value = oShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\Release")

'Check for .NET Framework 4.5.1 -> 378xxx
If ((Err.Number <> 0) Or (Clng(Value) < 379000)) Then
'.NET 4.5.1 is not istalled....install it (and wait for completition)
  MsgBox("Installing .NET Framework 4.5.1...")
  oShell.Run """<< ONLY ABSOLUTE PATH TO YOUR PACKAGE FILE + BACKSLASH >>" + PackageName + "", 1, False
End If

Do
  WScript.Sleep 500
Loop Until IsProcessRunning(PackageName) = False

MsgBox ".NET Framework 4.5.1 has been installed successfully, Running MyApp..."
oShell.Exec("MyApp.exe")

此循环仅使用0 - 3%的CPU资源,并且肯定会因任何原因等待安装程序完成或中断。

注意:此选项仅在主.NET Framework安装程序包作为后台进程运行时才有效,直到安装完成或中断为止。

在这里,当您安装.NET Framework 4.5.1时,此选项应该可以正常工作。