Inability to read a Registry key with + in name

lmilesuk

New member
Joined
May 19, 2012
Messages
2
Programming Experience
1-3
Hi
I'm having troubles reading the values from a registy key,
basically I already have my code working which reads back data from a registry
key with the name "Printer" however I also have another key called "Printer+" and
when I attempt to read that the "+" get ignored and it reads back "data" which
is in the same location. Is there a simple way of reading it back which won't
mean large changes to code that is already working as I need it to?

Any help would be greatly appreciated

VB.NET:
'READ A VALUE FROM THE COMPUTERS REGISTRY
    'function that reads preset registry keys
    Public Sub getPrinterInfo()

        ReDim PrinterData(arrayLimit)

        Dim regKey As RegistryKey

        Dim searchKey As RegistryKey = _
        Registry.LocalMachine.OpenSubKey("SYSTEM\CurrentControlSet\Control\Print\Printers", RegistryKeyPermissionCheck.ReadSubTree)

        Dim ver As String = ""
        Dim count As Byte = 0
        Dim i As Byte = 0

        Try

            For Each x In searchKey.GetSubKeyNames
                regKey = Registry.LocalMachine.OpenSubKey("SYSTEM\CurrentControlSet\Control\Print\Printers\" & x, RegistryKeyPermissionCheck.ReadSubTree)
                ver = regKey.GetValue("Printer Driver")

                
                If ver = "Test Printer" Or ver = "Test Printer+" Then

                    PrinterData(count).friendlyName = x
                    PrinterData(count).driverVersion = ver
                    PrinterData(count).friendlyPort = getPort(x)
                    PrinterData(count).serverDataIndex = -1
                    PrinterData(count).Connected = connectionCheck(x)

                    regKey = Registry.LocalMachine.OpenSubKey("SYSTEM\CurrentControlSet\Control\Print\Printers\" & x & "\PrinterDriverData", RegistryKeyPermissionCheck.ReadSubTree)
                    PrinterData(count).firmwareVersion = getVersionNumber(regKey.GetValue("ES_FWVersion"))

                 
                    While i < serverData.Count
                        If PrinterData(count).driverVersion = serverData(i).productName Then
                            PrinterData(count).serverDataIndex = i
                            i = serverData.Count
                        End If

                        i = i + 1
                    End While

                    i = 0

                    If PrinterData(count).serverDataIndex = -1 Then 
                        MsgBox(Translator.Translate("Server data error, No data available for printer", translations), MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation, Translator.Translate("No data available for printer", translations) & "[" & PrinterData(count).friendlyName & "]")
                    ElseIf PrinterData(count).firmwareVersion < serverData(PrinterData(count).serverDataIndex).latestFirmware Then
                        PrinterData(count).outOfDate = True
                    End If

                    count = count + 1

                End If

                regKey.Close()

            Next

            ReDim Preserve PrinterData((count - 1))

            'close reg key handle
            searchKey.Close()

        Catch Ex As Exception
            debugGenerateOutput("getPrinterInfo" & " :: " & Ex.Message & vbCrLf & "-STACK TRACE-" & vbCrLf & Ex.StackTrace)
        Finally
            If debug = 1 Then
                debugGenerateOutput(vbCrLf & vbCrLf & "Start getPrinterInfo debug")
                debugGenerateOutput(vbTab & "Loop counter: " & count)
                debugGenerateOutput("End getPrinterInfo debug")
            End If
        End Try
 
Last edited:
I'm having troubles reading the values from a registy key,
basically I already have my code working which reads back data from a registry
key with the name "Printer" however I also have another key called "Printer" and
when I attempt to read that the "+" get ignored and it reads back "data" which
is in the same location.
Something is missing in your description, can you clarify? I don't see a problem with + chars in key names however.

In your code you can also simplify, instead of looking up the whole path you can open the subkeys directly:
Dim printer = searchKey.OpenSubKey(x, False)

and
Dim data = printer.OpenSubKey("PrinterDriverData", False)
 
The line where it reads the ver is the problem, I am unable to read the + as a string? I'm trying to update some exsting working code. How would I go about doing this in a simple way??

'READ A VALUE FROM THE COMPUTERS REGISTRY
'function that reads preset registry keys
Public Sub getPrinterInfo()
ReDim PrinterData(arrayLimit)
Dim regKey As RegistryKey
Dim searchKey As RegistryKey = _
Registry.LocalMachine.OpenSubKey("SYSTEM\CurrentControlSet\Control\Print\Printers", RegistryKeyPermissionCheck.ReadSubTree)
Dim ver As String = ""
Dim count As Byte = 0
Dim i As Byte = 0
Try
For Each x In searchKey.GetSubKeyNames
regKey = Registry.LocalMachine.OpenSubKey("SYSTEM\CurrentControlSet\Control\Print\Printers\" & x, RegistryKeyPermissionCheck.ReadSubTree)
ver = regKey.GetValue("Printer Driver")


If ver = "Printer" Or ver = "Printer+" Or Then


PrinterData(count).friendlyName = x
PrinterData(count).driverVersion = ver
PrinterData(count).friendlyPort = getPort(x)
PrinterData(count).serverDataIndex = -1
PrinterData(count).Connected = connectionCheck(x)
regKey = Registry.LocalMachine.OpenSubKey("SYSTEM\CurrentControlSet\Control\Print\Printers\" & x & "\PrinterDriverData", RegistryKeyPermissionCheck.ReadSubTree)
PrinterData(count).firmwareVersion = getVersionNumber(regKey.GetValue("ES_FWVersion"))

While i < serverData.Count
If PrinterData(count).driverVersion = serverData(i).productName Then
PrinterData(count).serverDataIndex = i
i = serverData.Count
End If
i = i + 1
End While
i = 0
If PrinterData(count).serverDataIndex = -1 Then
'IMPROVE ERROR MESSAGE!!
MsgBox(Translator.Translate("Server data error, No data available for printer", translations), MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation, Translator.Translate("No data available for printer", translations) & "[" & PrinterData(count).friendlyName & "]")
ElseIf PrinterData(count).firmwareVersion < serverData(PrinterData(count).serverDataIndex).latestFirmware Then
PrinterData(count).outOfDate = True
End If
count = count + 1
End If
regKey.Close()
Next
ReDim Preserve PrinterData((count - 1))
'close reg key handle
searchKey.Close()
Catch Ex As Exception
debugGenerateOutput("getPrinterInfo" & " :: " & Ex.Message & vbCrLf & "-STACK TRACE-" & vbCrLf & Ex.StackTrace)
Finally
If debug = 1 Then
debugGenerateOutput(vbCrLf & vbCrLf & "Start getPrinterInfo debug")
debugGenerateOutput(vbTab & "Loop counter: " & count)
debugGenerateOutput("End getPrinterInfo debug")
End If
End Try
 
The line where it reads the ver is the problem, I am unable to read the + as a string?
If value is "Printer+" it will detect it, so if it doesn't then that is not the value.

Also note that your thread title is "Inability to read a Registry key with + in name", but the string you're referring to here is the string value of a name-value pair.
 

Latest posts

Back
Top