问题
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.
回答1:
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.
回答2:
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..
回答3:
I think you need you need to do this:
regKey = My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE",True).OpenSubKey("APP_NAME", True)
来源:https://stackoverflow.com/questions/3043714/change-a-registry-value-in-vb-net