johnnyg123
asked on
error trying to retrieve registry key info in vb.net app
the following code worked fine on my windows xp box
Dim regKey As RegistryKey
Dim regConnectionString As String
regKey = Registry.LocalMachine.Open SubKey("SO FTWARE\Emp loyee")
regConnectionString = regKey.GetValue("Connectio nString", "")
If regConnectionString <> "" Then
m_sConnekStrg = regConnectionString
Else
Throw New Exception("Error reading registry: Employee Connection String.")
End If
regKey.Close()
but when I try to execute on my windows 7 box I get an object not set to a reference
when trying to execute regConnectionString = regKey.GetValue("Connectio nString", "")
I get
Dim regKey As RegistryKey
Dim regConnectionString As String
regKey = Registry.LocalMachine.Open
regConnectionString = regKey.GetValue("Connectio
If regConnectionString <> "" Then
m_sConnekStrg = regConnectionString
Else
Throw New Exception("Error reading registry: Employee Connection String.")
End If
regKey.Close()
but when I try to execute on my windows 7 box I get an object not set to a reference
when trying to execute regConnectionString = regKey.GetValue("Connectio
I get
Or if it's a windows application it's app.config.
ASKER
I don't like getting things from registry and normally do use config file but this app was written many moons ago and recently I have inherited support
For Windows 7, try using:
regKey = Registry.LocalMachineRegis try.OpenSu bKey("Soft ware", True)
regKey = Registry.LocalMachineRegis
Dim regKey As RegistryKey
Dim regConnectionString As String
'regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Employee")
regKey = Registry.LocalMachineRegistry.OpenSubKey("Software\Employee", True)
regConnectionString = regKey.GetValue("ConnectionString", "")
If regConnectionString <> "" Then
m_sConnekStrg = regConnectionString
Else
Throw New Exception("Error reading registry: Employee Connection String.")
End If
regKey.Close()
Actually, belay my last. Are you running a 64bit version of Windows 7?
-saige-
-saige-
ASKER
I did forget to mention I am running 64 bit windows 7
In case you are not using a 64-bit version of Windows 7?
The appropriate code would be:
-saige-
The appropriate code would be:
Dim regKey As RegistryKey
Dim regConnectionString As String
' Not this...
' regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Employee")
' Specify that you do not need write access to the key using
regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Employee", False)
regConnectionString = regKey.GetValue("ConnectionString", "")
If regConnectionString <> "" Then
m_sConnekStrg = regConnectionString
Else
Throw New Exception("Error reading registry: Employee Connection String.")
End If
regKey.Close()
HTH,-saige-
In the case of 64-bit, the LocalMachine\Software hive is no longer located under,
\\HKEY_LOCAL_MACHINE\Softw are
This is now allocated as the location of 64-bit applications. To find settings for 32-bit software, you would go one level further into:
\\HKEY_LOCAL_MACHINE\Softw are\WOW643 2Node
So, here are the solutions. You can:
1. Recompile the application as a 64-bit application
-or-
2. Add code into your method to first, distunguish the operating system and then based upon the operating system go to the correct hive location.
HTH,
-saige-
\\HKEY_LOCAL_MACHINE\Softw
This is now allocated as the location of 64-bit applications. To find settings for 32-bit software, you would go one level further into:
\\HKEY_LOCAL_MACHINE\Softw
So, here are the solutions. You can:
1. Recompile the application as a 64-bit application
-or-
2. Add code into your method to first, distunguish the operating system and then based upon the operating system go to the correct hive location.
HTH,
-saige-
And when I say determine the operating system, I do not mean the OS by name but rather determine if it is an x86 application or x64 application.
Let say you used something like this:
-saige-
Let say you used something like this:
Dim osBit As Integer = System.Runtime.InteropServices.Marshal.SizeOf(New IntPtr)
Dim regKey As RegistryKey
Dim regConnectionString As String
' Not this...
' regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Employee")
' Specify that you do not need write access to the key using
Select Case osBit
' 32-bit Version
Case 4
regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Employee", False)
regConnectionString = regKey.GetValue("ConnectionString", "")
' 64-bit Version
Case 8
regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Employee", False)
regConnectionString = regKey.GetValue("ConnectionString", "")
Case Else
' Add fall-out code in case of unsupported or undefined operating system
End Select
If regConnectionString <> "" Then
m_sConnekStrg = regConnectionString
Else
Throw New Exception("Error reading registry: Employee Connection String.")
End If
regKey.Close()
HTH,-saige-
ASKER
ok....I can try that
Guess I would have thought it would just tell me there is no such key rather than give me an object without reference error
Guess I would have thought it would just tell me there is no such key rather than give me an object without reference error
Sometimes you can get too far ahead of yourself, especially when you dont have Visual Studio open and your just using notepad... *sigh* :P
This is what I meant:
-saige-
This is what I meant:
Dim osBit As Integer = System.Runtime.InteropServices.Marshal.SizeOf(New IntPtr)
Dim regKey As RegistryKey
Dim regConnectionString As String
' Not this...
' regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Employee")
' Specify that you do not need write access to the key using
Select Case osBit
' 32-bit Version
Case 4
regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Employee", False)
regConnectionString = regKey.GetValue("ConnectionString", "")
' 64-bit Version
Case 8
regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\WOW6432Node\Employee", False)
regConnectionString = regKey.GetValue("ConnectionString", "")
Case Else
' Add fall-out code in case of unsupported or undefined operating system
End Select
If regConnectionString <> "" Then
m_sConnekStrg = regConnectionString
Else
Throw New Exception("Error reading registry: Employee Connection String.")
End If
regKey.Close()
HTH,-saige-
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi,
The problem is Win32.Registry namespace doesn't have have the new view flags available until .NET Framework 4.0. If you use 4.0 you can use the code examples below, prior to .NET framework 4.0 you have to use P/Invoke RegOpenKeyEx() see Accessing an Alternate Registry View
The problem is Win32.Registry namespace doesn't have have the new view flags available until .NET Framework 4.0. If you use 4.0 you can use the code examples below, prior to .NET framework 4.0 you have to use P/Invoke RegOpenKeyEx() see Accessing an Alternate Registry View
' Read 64 bit view of registry from 32 bit application running on 64 bit OS
Dim regkey As RegistryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64).OpenSubKey("SOFTWARE\Medieval")
Debug.Print(regkey.GetValue("Test").ToString)
regkey.Close()
' Read 32 bit view of registry from 64 bit application running on 64 bit OS
Dim regkey As RegistryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey("SOFTWARE\Medieval")
Debug.Print(regkey.GetValue("Test").ToString)
regkey.Close()
http://articles.sitepoint.com/article/web-config-file-demystified