Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking if Registry Key's or Value's exist

General

Checking if Registry Key's or Value's exist

by  djhawthorn  Posted    (Edited  )
Below are three functions which check
- If a registry KEY exists
- If a registry VALUE exists
- If a registry item exists (key or value, depending on what you pass it).

I found it very easy to check if a registry value existed, but checking if a registry key existed wasn't so easy. I found someone who had worked out a fix for this problem, so I cleaned it up a bit, and its preseted below for anyones reference :)

Code:
'************************************
'*
'* Registry Key Exists (Function)
'* Version 1.1
'*
'************************************
'This function checks to see if a passed registry key exists, and
'returns true if it does
'
'Requirements: The registry key you are looking for (RegistryKey)
Function RegistryKeyExists (fncStrRegistryKey)
  'Ensure the last character is a backslash (\) - if it isn't, add one
  If (Right(fncStrRegistryKey, 1) <> "\") Then
    'Add a backslash
    fncStrRegistryKey = fncStrRegistryKey & "\"
  End If

  'If there isnt the key when we read it, it will return an error, so we need to resume
  On Error Resume Next

  'Try reading the key
  WshShell.RegRead fncStrRegistryKey

  'Catch the error
  Select Case Err
    'Error Code 0 = 'success'
    Case 0
      fncStrRegistryKeyExists = true
    'This checks for the (Default) value existing (but being blank); as well as key's not existing at all (same error code)
    Case &h80070002
      'Read the error description, removing the registry key from that description
      strErrDescription = Replace(Err.description, fncStrRegistryKey, "")

      'Clear the error
      Err.clear

      'Read in a registry entry we know doesn't exist (to create an error description for something that doesnt exist)
      WshShell.RegRead "HKEY_ERROR\"

      'The registry key exists if the error description from the HKEY_ERROR RegRead attempt doesn't match the error
      'description from our fncStrRegistryKey RegRead attempt
      If (strErrDescription <> Replace(Err.description, "HKEY_ERROR\", "")) Then
        RegistryKeyExists = true
      Else
        RegistryKeyExists = false
      End If
    'Any other error code is a failure code
    Case Else
      RegistryKeyExists = false
  End Select

  'Turn error reporting back on
  On Error Goto 0
End Function


Code:
'************************************
'*
'* Registry Value Exists (Function)
'* Returns a value (true / false)
'*
'************************************
'This function checks to see if a passed registry value exists, and
'returns true if it does
'
'Requirements: The registry value you are looking for (RegistryValue)
Function RegistryValueExists (fncStrRegistryValue)
  'Ensure the last character is NOT a backslash (\) - if it is, we aren't looking for a value
  If (Right(fncStrRegistryValue, 1) = "\") Then
    'It's not a registry value we are looking for
    RegistryValueExists = false
  Else
    'If there isnt the value when we read it, it will return an error, so we need to resume
    On Error Resume Next

    'Try reading the value
    WshShell.RegRead fncStrRegistryValue

    'Catch the error
    Select Case Err
      Case 0:
        'Error Code 0 = 'success'
        RegistryValueExists = true
      Case Else
        'Any other error code is a failure code
        RegistryValueExists = false
    End Select

    'Turn error reporting back on
    On Error Goto 0
  End If
End Function


Code:
'************************************
'*
'* Registry Item Exists (Function)
'* Version 1.1
'*
'************************************
'This function checks to see if a passed registry key/value exists, and
'returns true if it does
'
'Requirements: The registry key/value you are looking for (fncStrRegistryItem)
'Note: RegistryItem MUST end in a backslash (\) if you are looking for a key
'      RegistryItem's without a backslash (\) will assume you are looking for a value
Function RegistryItemExists (fncStrRegistryItem)
  'If there isnt the item when we read it, it will return an error, so we need to resume
  On Error Resume Next

  'Find out if we are looking for a key or a value
  If (Right(fncStrRegistryItem, 1) = "\") Then
    'It's a registry key we are looking for
    'Try reading the key
    WshShell.RegRead fncStrRegistryItem

    'Catch the error
    Select Case Err
      'Error Code 0 = 'success'
      Case 0:
        RegistryItemExists = true
      'This checks for the (Default) value existing (but being blank); as well as key's not existing at all (same error code)
      Case &h80070002:
        'Read the error description, removing the registry key from that description
        ErrDescription = Replace(Err.description, fncStrRegistryItem, "")

        'Clear the error
        Err.clear

        'Read in a registry entry we know doesn't exist (to create an error description for something that doesnt exist)
        WshShell.RegRead "HKEY_ERROR\"

        'The registry key exists if the error description from the HKEY_ERROR RegRead attempt doesn't match the error
        'description from our RegistryKey RegRead attempt
        If (ErrDescription <> Replace(Err.description, "HKEY_ERROR\", "")) Then
          RegistryItemExists = true
        Else
          RegistryItemExists = false
        End If
      'Any other error code is a failure code
      Case Else:
        RegistryItemExists = false
    End Select
  Else
    'It's a registry value we are looking for
    'Try reading the value
    WshShell.RegRead fncStrRegistryItem

    'Catch the error
    Select Case Err
      Case 0:
        'Error Code 0 = 'success'
        RegistryItemExists = true
      Case Else
        'Any other error code is a failure code
        RegistryItemExists = false
    End Select
  End If

  'Turn error reporting back on
  On Error Goto 0
End Function
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top