Check if registry key exists using VBScript

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

    See the Scripting Guy! Blog:

    How Can I Tell Whether a Value Exists in the Registry?

    They discuss doing the check on a remote computer and show that if you read a string value from the key, and if the value is Null (as opposed to Empty), the key does not exist.

    With respect to using the RegRead method, if the term "key" refers to the path (or folder) where registry values are kept, and if the leaf items in that key are called "values", using WshShell.RegRead(strKey) to detect key existence (as opposed to value existance) consider the following (as observed on Windows XP):

    If strKey name is not the name of an existing registry path, Err.Description reads "Invalid root in registry key"... with an Err.Number of 0x80070002.

    If strKey names a registry path that exists but does not include a trailing "\" the RegRead method appears to interpret strKey as a path\value reference rather than as a simple path reference, and returns the same Err.Number but with an Err.Description of "Unable to open registry key". The term "key" in the error message appears to mean "value". This is the same result obtained when strKey references a path\value where the path exists, but the value does not exist.

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-29 09:12

    I found the solution.

    dim bExists
    ssig="Unable to open registry key"
    
    set wshShell= Wscript.CreateObject("WScript.Shell")
    strKey = "HKEY_USERS\.Default\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Digest\"
    on error resume next
    present = WshShell.RegRead(strKey)
    if err.number<>0 then
        if right(strKey,1)="\" then    'strKey is a registry key
            if instr(1,err.description,ssig,1)<>0 then
                bExists=true
            else
                bExists=false
            end if
        else    'strKey is a registry valuename
            bExists=false
        end if
        err.clear
    else
        bExists=true
    end if
    on error goto 0
    if bExists=vbFalse then
        wscript.echo strKey & " does not exist."
    else
        wscript.echo strKey & " exists."
    end if
    
    0 讨论(0)
  • 2020-12-29 09:16

    The second of the two methods here does what you're wanting. I've just used it (after finding no success in this thread) and it's worked for me.

    http://yorch.org/2011/10/two-ways-to-check-if-a-registry-key-exists-using-vbscript/

    The code:

    Const HKCR = &H80000000 'HKEY_CLASSES_ROOT
    Const HKCU = &H80000001 'HKEY_CURRENT_USER
    Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE
    Const HKUS = &H80000003 'HKEY_USERS
    Const HKCC = &H80000005 'HKEY_CURRENT_CONFIG
    
    Function KeyExists(Key, KeyPath)
        Dim oReg: Set oReg = GetObject("winmgmts:!root/default:StdRegProv")
        If oReg.EnumKey(Key, KeyPath, arrSubKeys) = 0 Then
            KeyExists = True
        Else
            KeyExists = False
       End If
    End Function
    
    0 讨论(0)
  • 2020-12-29 09:20

    In case anyone else runs into this, I took WhoIsRich's example and modified it a bit. When calling ReadReg I needed to do the following: ReadReg("App", "HKEY_CURRENT_USER\App\Version") which would then be able to read the version number from the registry, if it existed. I also am using HKCU since it does not require admin privileges to write to.

    Function ReadReg(RegKey, RegPath)
          Const HKEY_CURRENT_USER = &H80000001
          Dim objRegistry, oReg
          Set objRegistry = CreateObject("Wscript.shell")
          Set oReg = GetObject("winmgmts:!root\default:StdRegProv")
    
          if oReg.EnumKey(HKEY_CURRENT_USER, RegKey) = 0 Then
            ReadReg = objRegistry.RegRead(RegPath)
          else
            ReadReg = ""
          end if
    End Function
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题