[RESOLVED] Object reference not set to an instance of an object
For some reason, on the line where I try to set the value of the key, I get the following error:
Object reference not set to an instance of an object
vb Code:
Private Sub writeToKey(ByVal regValue As String)
Dim regkey As RegistryKey = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows NT\CurrentVersion\Windows\Device")
regkey.SetValue("Device", regValue, RegistryValueKind.String) 'error occurs here
regkey.Close()
End Sub
What am I doing wrong?
Re: Object reference not set to an instance of an object
It means that Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows NT\CurrentVersion\Windows\Device") = Nothing
Thus, regkey = Nothing, which means you can't set the value of a Nothing.
Re: Object reference not set to an instance of an object
In other words:
the registrykey doesn't exist or you don't have the right permissions to open it.
Re: Object reference not set to an instance of an object
Ok, I think what I need to do is change that line of code to:
Dim regkey As RegistryKey = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows NT\CurrentVersion\Windows\")
But now it fails with the message that it cannot write to the key. Is there something wrong with the following line? I'm trying to change the value of the key HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device. This is the key for the default printer.
regkey.SetValue("Device", regValue, RegistryValueKind.String) 'fails here
Re: Object reference not set to an instance of an object
Are you sure you have permission to change that key? Is this being run on a Vista machine?
Re: Object reference not set to an instance of an object
Ooops! Forgot to use "True" at the end to make the key writable...
Dim regkey As RegistryKey = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows NT\CurrentVersion\Windows", True)