Check if registry key exists using VBScript

后端 未结 7 1963
情话喂你
情话喂你 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:22

    Simplest way avoiding RegRead and error handling tricks. Optional friendly consts for the registry:

    Const HKEY_CLASSES_ROOT   = &H80000000
    Const HKEY_CURRENT_USER   = &H80000001
    Const HKEY_LOCAL_MACHINE  = &H80000002
    Const HKEY_USERS          = &H80000003
    Const HKEY_CURRENT_CONFIG = &H80000005
    

    Then check with:

    Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
    
    If oReg.EnumKey(HKEY_LOCAL_MACHINE, "SYSTEM\Example\Key\", "", "") = 0 Then
      MsgBox "Key Exists"
    Else
      MsgBox "Key Not Found"
    End If
    

    IMPORTANT NOTES FOR THE ABOVE:

    • There are 4 parameters being passed to EnumKey, not the usual 3.
    • Equals zero means the key EXISTS.
    • The slash after key name is optional and not required.
    0 讨论(0)
提交回复
热议问题