views:

151

answers:

3

I have searched a lot to find a solution for my problem, but I can't find any specific solution.

I need to change a value which is stored under HKEY_LOCAL_MACHINE\SOFTWARE\APP_NAME. It is not possible to use .NET's registry functions, since they have drastic restrictions.

The following snippet should solve my problem:

Dim regKey As RegistryKey
Dim ver As Decimal
regKey = My.Computer.Registry.LocalMachine.OpenSubKey("HKEY_LOCAL_MACHINE\SOFTWARE\APP_NAME", True)

regKey.SetValue("IP", "192.168.10.15:18500") regKey.Close()

Unfortunately, I get a NullReferenceExeption when I try to set the value.

What should I do? I have imported Microsoft.Win32 and RegistryKey doesn't accept any constructor.

+2  A: 

Does the subkey exists? Quote from MSDN: Rather than throwing an exception, a null reference (Nothing in Visual Basic) is returned if the requested key does not exist..

ho1
The Subkey exists for shure. I have used two different ways to check this.
mhofer
+2  A: 

I think you need you need to do this:

regKey = My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE",True).OpenSubKey("APP_NAME", True)
Barry
@Barry:Thanks for your reply. I am now able to store the key, but I can't see the change in the Windows Registry Editor.When I use my function to read the value, I get the right value. Is there something different when using Windows 7? I had already some troubles with the VirtualStore...
mhofer
@mhofer TBH I'm not sure, it works for Win XP but I haven't got a Win 7 box to test it on at the moment.
Barry
+1  A: 

Remove "HKEY_LOCAL_MACHINE" from the key name, that's already covered by the LocalMachine member in your code.

Beware that this code will not work on a regular Vista or Win7 machine, you cannot open this key for writing with UAC enabled. You'd need a manifest that requires administrator privileges. Write to My.Computer.Registry.CurrentUser instead.

One more complication is registry virtualization if you run this on the 64-bit version of Windows. 32-bit programs will read and write HKLM\Software keys to/from HKLM\Software\Wow6432Node instead.

Hans Passant
Thanks for your answer. It is really what I've expected, Windows 7 causes the problem.I need to run my appliaction with administrator privileges, since the key MUST be stored under "HKEY_LOCAL_MACHINE" (it's a third party application).
mhofer