Check if registry key exists using VBScript

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

    edit (sorry I thought you wanted VBA).

    Anytime you try to read a non-existent value from the registry, you get back a Null. Thus all you have to do is check for a Null value.

    Use IsNull not IsEmpty.

    Const HKEY_LOCAL_MACHINE = &H80000002
    
    strComputer = "."
    Set objRegistry = GetObject("winmgmts:\\" & _ 
        strComputer & "\root\default:StdRegProv")
    
    strKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion"
    strValueName = "Test Value"
    objRegistry.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
    
    If IsNull(strValue) Then
        Wscript.Echo "The registry key does not exist."
    Else
        Wscript.Echo "The registry key exists."
    End If
    

提交回复
热议问题