I'm needing to read a file location value from the Local_Machine section of the registry.
On my Win 7 64 bit O/S my code will not return a value for my key, but does return values for an equivalent microsoft key.
Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load TestRegValue() End Sub ' Form1_Load Sub TestRegValue() Dim sAns As String = "" Dim sErr As String = "" sAns = RegValue(RegistryHive.LocalMachine, "SOFTWARE\Microsoft\Windows\CurrentVersion", "ProgramFilesDir", sErr) If sAns <> "" Then MsgBox("Value = " & sAns) Else MsgBox("This error occurred: " & sErr) End If sAns = RegValue(RegistryHive.LocalMachine, "SOFTWARE\CADApps\Landscape", "Data File", sErr) If sAns <> "" Then MsgBox("Value = " & sAns) Else MsgBox("This error occurred: " & sErr) End If End End Sub Public Function RegValue(ByVal Hive As RegistryHive, _ ByVal Key As String, ByVal ValueName As String, Optional ByRef ErrInfo As String = "") As String Dim objParent As RegistryKey = Registry.LocalMachine Dim objSubkey As RegistryKey = Nothing Dim sAns As String = "" ' objParent = Registry.LocalMachine 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 '' Snipped code End Class
The registry entries seem equivalent as can be seen on the screen grab showing that the 'CADApps\Landscape' key exists.
In addition to the above code, slightly modified from code at:
I have tried numerous other code snippets to read this value and all of them will not return a value for the CADApps entry.
It this an aspect of the 64 bit O/S ?
The default Security settings for the key seen in the registry "Advanced Security Settings" allow the key to be read. Changing them to "Full control" has no effect.
Solved! Go to Solution.
In addition to Josh's findings, here's a link to some code (albeit c# not VB) that should help you get the data needed:
I was easily able to convert the C# code at the Fusion site.
A few modifications produced the code below. Running this code, confirmed Josh's reply (of which I had some vague feeling, but did not realise that when I looked at the registry I was looking at the 64 bit section, while my code was trying to read the 32 bit section.)
Sub GetRegistryValue() Dim sValue As String = ReadFromLocalMachine("SOFTWARE\CADApps\Landscape", "Data File") End Sub Private Shared Function ReadFromLocalMachine(ByVal spRegKeyName As String, ByVal spValueName As String) As String Dim value64 As String = String.Empty Dim value32 As String = String.Empty Dim localKey As RegistryKey = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHi
ve.LocalMachine, RegistryView.Registry64) localKey = localKey.OpenSubKey(spRegKeyName) If localKey IsNot Nothing Then value64 = localKey.GetValue(spValueName).ToString() End If Dim localKey32 As RegistryKey = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHi ve.LocalMachine, RegistryView.Registry32) localKey32 = localKey32.OpenSubKey(spRegKeyName) If localKey32 IsNot Nothing Then value32 = localKey32.GetValue(spValueName).ToString() End If Return value64 & " AAA " & value32 End Function ' ReadFromLocalMachine