Check if registry key exists using VBScript

后端 未结 7 1964
情话喂你
情话喂你 2020-12-29 08:36

I thought this would be easy, but apparently nobody does it... I\'m trying to see if a registry key exists. I don\'t care if there are any values inside of it such as (Defau

7条回答
  •  礼貌的吻别
    2020-12-29 09:09

    The accepted answer is too long, other answers didn't work for me. I'm gonna leave this for future purpose.

    Dim sKey, bFound
    skey = "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\SecurityHealth"
    
    with CreateObject("WScript.Shell")
      on error resume next            ' turn off error trapping
        sValue = .regread(sKey)       ' read attempt
        bFound = (err.number = 0)     ' test for success
      on error goto 0                 ' restore error trapping
    end with
    
    If bFound Then
      MsgBox = "Registry Key Exist."
    Else
      MsgBox = "Nope, it doesn't exist."
    End If
    

    Here's the list of the Registry Tree, choose your own base on your current task.

    HKCR = HKEY_CLASSES_ROOT
    HKCU = HKEY_CURRENT_USER
    HKLM = HKEY_LOCAL_MACHINE
    HKUS = HKEY_USERS
    HKCC = HKEY_CURRENT_CONFIG
    

提交回复
热议问题