0

嘿,伙计们,我知道这听起来可能很愚蠢,但我脑子里一直有这个问题……我对这个 wscript 或 vbscripting 真的很陌生……在撰写本文时,我想出了如何使用 wscript 打开 IE .. .这是代码

Set WshShell = WScript.CreateObject("WScript.Shell")
Return = WshShell.Run("iexplore.exe www.bbc.co.uk", 1)

但我不知道如何检查是否安装了firefox,然后打开firefox,如果安装了chrome,打开chrome,所有浏览器类型都一样.....

更新:

我做了一些研究,想为什么不检查注册表,所以我想出了这个检查注册表的脚本,现在我不知道为什么,但这总是给出相同的输出“密钥不存在”事件虽然我有这个我系统中的注册表

keyTest = keyExists("HKEY_LOCAL_MACHINE\SOFTWARE\Mozilla\Mozilla Firefox")
If keyTest = False Then
    wscript.echo "Key does not exist"
Elseif keyTest = True then
    wscript.echo "Key exists"
End if

Function keyExists (RegistryKey)
    If (Right(RegistryKey, 1) <> "\") Then
        RegistryKeyExists = false
    Else
        On Error Resume Next
        WshShell.RegRead RegistryKey
        Select Case Err

            Case 0:
                keyExists = true
            Case &h80070002:
                ErrDescription = Replace(Err.description, RegistryKey, "")
                Err.clear
                WshShell.RegRead "HKEY_ERROR\"
            If (ErrDescription <> Replace(Err.description, _
            "HKEY_ERROR\", "")) Then
                keyExists = true
            Else
                RegistryKeyExists = false
            End If
            Case Else:
                keyExists = false
        End Select
        On Error Goto 0
    End If
End Function
4

2 回答 2

0

您的示例中的问题:

  • keyExists()中,一个名为的变量RegistryKeyExists正被用于函数的返回值keyExists

  • Shell 对象变量WshShell永远不会通过CreateObject().

  • 感兴趣的注册表项的值不以反斜杠结尾。

这是您的代码的简化版本,我相信它可以实现您的目标:

Option Explicit  ' programming with your seatbelt on :-)

Dim keys(4)
keys(0) = "HKEY_LOCAL_MACHINE\SOFTWARE\Mozilla\Mozilla Firefox"
keys(1) = "HKEY_LOCAL_MACHINE\SOFTWARE\Mozilla\Mozilla Firefox\"
keys(2) = "HKEY_LOCAL_MACHINE\Bad\Key\"
keys(3) = "BAD\Root\On\This\Key\Causes\Exception"
keys(4) = "HKLM\SOFTWARE\Microsoft\Internet Explorer\"

On Error Resume Next

Dim i, key
For i = 0 To UBound(keys)
    key = keyExists(keys(i))

    If Err Then
        WScript.Echo "An exception occurred reading registry key" _
               & " '" & keys(i) & "':" _
               & " [" & Err.Number & "] " _
               & Err.Description _
               & ""
    Else
        If keyExists(keys(i)) Then
            WScript.Echo "Key *exists*: [" & keys(i) & "]"
        Else
            WScript.Echo "Key does *not* exist: [" & keys(i) & "]"
        End If
    End If

    WScript.Echo "--"
Next

Function keyExists (RegistryKey)
    Dim keyVal, errNum, errDesc

    keyExists = False
    On Error Resume Next

    Dim WshShell : Set WshShell = CreateObject("WScript.Shell")
    keyVal = WshShell.RegRead(RegistryKey)

    Select Case Err
        Case 0
            keyExists = True

        Case &h80070002
            ' key does not exist

        Case Else
            errNum = Err.Number
            errDesc = Err.Description
            On Error GoTo 0
            Err.Raise vbObjectError + 1, "WScript.Shell", _
               "Something went wrong reading the registry:" _
               & " [" & Hex(errNum) & "] " & errDesc
    End Select

    On Error GoTo 0
    Set WshShell = Nothing
End Function

' End
于 2012-07-07T02:04:14.147 回答
0

通常可以使用以下代码来查找获取所有已安装软件的列表。这里我使用了Message box来显示这个列表,你可以使用if条件来判断是否安装了想要的软件............

' List All Installed Software
  Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE
  strComputer = "."
  strKey = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"
  strEntry1a = "DisplayName"


  Set objReg = GetObject("winmgmts://" & strComputer & _
   "/root/default:StdRegProv")
  objReg.EnumKey HKLM, strKey, arrSubkeys

  For Each strSubkey In arrSubkeys
      intRet1 = objReg.GetStringValue(HKLM, strKey & strSubkey, _
      strEntry1a, strValue1)

      If strValue1 <> "" Then
          MsgBox  VbCrLf & "Display Name: " & strValue1
      End If
  Next

我已经在机器上尝试过这段代码,发现它只是列出了 Firefox 浏览器,即使我安装了 chrome 和 IE。所以这种常规方法对每个人都不起作用。之后我检查了注册表,发现所有浏览器都列在......

HKEY_LOCAL_MACHINE\SOFTWARE\Clients\StartMenuInternet\

所以我们可以编写代码来查找是否安装了特定的浏览器。以下示例代码检查是否安装了 Chrome 和 Firefox,如果已安装并通过 URL 打开它

Set WshShell = CreateObject("WScript.Shell")
Const HKEY_LOCAL_MACHINE = &H80000002 
strComputer = "." 

Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _ 
strComputer & "\root\default:StdRegProv") 

strKeyPath = "SOFTWARE\Clients\StartMenuInternet\chrome.exe\shell\open\command\" 
strValueName = "" 
oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue 
If InStr(1,strValue,"chrome",vbTextCompare) Then WshShell.Run("chrome www.google.com")


strKeyPath = "SOFTWARE\Clients\StartMenuInternet\FIREFOX.EXE\shell\open\command\" 
strValueName = "" 
oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue 
If InStr(1,strValue,"firefox",vbTextCompare) Then WshShell.Run("firefox www.google.com")

同样,您可以为 IE、Opera 和 Safari 修改此代码希望这会有所帮助.......

于 2012-07-07T21:49:01.990 回答