|
-
May 12th, 2009, 07:40 AM
#1
Thread Starter
Hyperactive Member
[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?
-
May 12th, 2009, 07:43 AM
#2
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.
-
May 12th, 2009, 08:12 AM
#3
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.
-
May 12th, 2009, 08:15 AM
#4
Thread Starter
Hyperactive Member
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
-
May 12th, 2009, 08:19 AM
#5
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?
-
May 12th, 2009, 08:30 AM
#6
Thread Starter
Hyperactive Member
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)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|