0

I'm trying to make something to clean my registry periodically and at an point, i need to read a value of one registry key, and it's there i'm having problems. The value is in BYTE (Hex) and i can't read it. Here's my code:

'function that i'm using to check if registry value exists and read it...

Public Function RegValue(ByVal Hive As RegistryHive, ByVal Key As String, ByVal ValueName As String, ByRef ErrInfo As String) As String
    Dim objParent As RegistryKey
    Dim objSubkey As RegistryKey
    Dim sAns As String
    Select Case Hive
        Case RegistryHive.ClassesRoot
            objParent = Registry.ClassesRoot
        Case RegistryHive.CurrentConfig
            objParent = Registry.CurrentConfig
        Case RegistryHive.CurrentUser
            objParent = Registry.CurrentUser
        Case RegistryHive.DynData
            objParent = Registry.DynData
        Case RegistryHive.LocalMachine
            objParent = Registry.LocalMachine
        Case RegistryHive.PerformanceData
            objParent = Registry.PerformanceData
        Case RegistryHive.Users
            objParent = Registry.Users
    End Select

    Try
        objSubkey = objParent.OpenSubKey(Key)
        'if can't be found, object is not initialized
        If Not objSubkey Is Nothing Then
            sAns = (objSubkey.GetValue(ValueName))
        End If

    Catch ex As Exception
        ErrInfo = ex.Message
    Finally

        'if no error but value is empty, populate errinfo
        If ErrInfo = "" And sAns = "" Then
            ErrInfo = "No value found for requested registry key"
        End If
    End Try
    Return sAns
End Function


' Button click that tries to read the value

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim sAns As String
    Dim sErr As String = ""

    sAns = RegValue(RegistryHive.ClassesRoot, "Local Settings\Software\Microsoft\Windows\Shell\BagMRU\0\0\0\0\2\0\0\0\0\0", "0", sErr)
    If sAns <> "" Then
        MsgBox("Value = " & sAns)
    Else
        MsgBox("This error occurred: " & sErr)
    End If
End Sub

When i run it and try to read the value, i get this message:

"This error occurred: The conversion of type 'Byte ()' to type 'String' is not valid."

There's a image of the registry value that i'm trying to read:

enter image description here

If someone can help, i would be thankful.

Happy
  • 13
  • 4
  • Does this answer your question? [How do you convert a byte array to a hexadecimal string, and vice versa?](https://stackoverflow.com/questions/311165/how-do-you-convert-a-byte-array-to-a-hexadecimal-string-and-vice-versa) – Daniel A. White Jan 10 '20 at 04:03
  • 1
    *"The value is in BYTE (Hex)"*. No it isn't. Hexadecimal is just a convenient way of displaying binary data. That has nothing to do with how it's stored. It's binary data, hence the type being `REG_BINARY`. Binary data is stored as a `Byte` array in VB, so when you read that value from the Registry you get a `Byte` array. How you work with that data depends on exactly what it represents. You could convert it to a hexadecimal `String` or a base-64 `String` or an encoded `String` or not a `String` at all. Why do you think that converting it to a `String` is a good idea? – jmcilhinney Jan 10 '20 at 04:11
  • idk, my plan is to do some system that check if a part of that byte array value exists on these registry values and if it does, deletes it – Happy Jan 10 '20 at 04:20

0 Answers0