Public Function TestForKeyValue(ByVal Reg_Key_Path As String, _
ByVal ValueToFind As String) As String
'****************************************************************************************************
'FUNCTION: tests if a value within a registry key exists
'Last Modified:
'****************************************************************************************************
Dim arrValueNames() As String
Dim iIndex As Int32
Dim reg_key As RegistryKey
Try
'open the key if it exists
reg_key = Registry.LocalMachine
If DoesKeyExist(Reg_Key_Path, reg_key) Then
'get all the values for the requested Key into array
arrValueNames = reg_key.GetValueNames()
'see if values is found in key
If Not arrValueNames Is Nothing Then
iIndex = Array.IndexOf(arrValueNames, ValueToFind)
If iIndex <> -1 Then 'IndexOf returns -1 when target is not found
Return reg_key.GetValue(ValueToFind)
End If
End If
Else
Return Nothing
End If
Catch nre As NullReferenceException
'capture the exception and use it to tell client app
'that there is no key w/ this name
Return Nothing
Catch e As Exception
Throw New Exception("The following error occured while testing the Registry Value", e)
Return Nothing
End Try
End Function
Private Function DoesKeyExist(ByVal Reg_Key_Path As String, _
ByRef Reg_Key As RegistryKey) As Boolean
'****************************************************************************************************
'FUNCTION: tests if a registry key exists
'Last Modified:
'****************************************************************************************************
Dim i, y As Int32
Dim bKey_Found As Boolean
Dim arrKeys_at_this_level(), arrPath() As String
Try
'break the path into components
arrPath = Reg_Key_Path.Split("\")
'drill down through path
For i = 0 To UBound(arrPath)
'get keys at the current level of the registry
arrKeys_at_this_level = Reg_Key.GetSubKeyNames
'reset flag
bKey_Found = False
For y = 0 To UBound(arrKeys_at_this_level) 'loop through this level
'if the
If arrKeys_at_this_level(y).ToUpper = arrPath(i).ToUpper Then
bKey_Found = True
Exit For
End If
Next
If bKey_Found Then
'if key is found then open it
Reg_Key = Reg_Key.OpenSubKey(arrPath(i))
Else
'some part of the path is broken ...
Return False
End If
Next
Return True
Catch nre As NullReferenceException
'capture the exception and use it to tell client app
'that there is no key w/ this name
Return False
Catch e As Exception
Throw New Exception("The following error occured while testing the Registry Key", e)
End Try
End Function