Writing to Registry: Unauthorized Access Exception

G

Guest

Hi All,

I am attempting to write values to the registry, but get an
UnauthorizedAccessException when I try to do a SetValue on an existing key.
However, I can create the key, with a value, when it does not exist.

Here is a snippet of the code:
Dim RegKey As RegistryKey
RegKey = Registry.LocalMachine.OpenSubKey(BaseKey & "\" &
SubKey, False)
If (RegKey Is Nothing) Then
RegKey = Registry.LocalMachine.CreateSubKey(BaseKey &
"\" & SubKey)
End If
Try
RegKey.SetValue("NumberRuns",
Tasks(I).NumberRuns.ToString())
RegKey.SetValue("TotalSeconds",
Tasks(I).TotalSeconds.ToString())
Catch ex As Exception
MessageBox.Show("Exception: " & ex.Message)
Finally
RegKey.Close()
End Try

What do I need to do to be able to update this key?

Thanks,
pagates
 
M

Mona

Hello Pegates,
How are you doing?

The error that you have specified could be due to lack of rights on the
machine. Try checking the rights of the user.

Also the OpenSubKey method has an overload that provides write access. Try
passing true to it, instead of False as you are doing:
RegKey = Registry.LocalMachine.OpenSubKey(BaseKey & "\" & SubKey,
False) ----- try passing True instead of False

I hope this helps.

Thanks
Mona
[Grapecity]
 
G

Guest

Hi Mona,

It was that stupid parameter...D'oh! (Yes, blame the parameter, not the
programmer) : )

Thanks again,
PAGates

Mona said:
Hello Pegates,
How are you doing?

The error that you have specified could be due to lack of rights on the
machine. Try checking the rights of the user.

Also the OpenSubKey method has an overload that provides write access. Try
passing true to it, instead of False as you are doing:
RegKey = Registry.LocalMachine.OpenSubKey(BaseKey & "\" & SubKey,
False) ----- try passing True instead of False

I hope this helps.

Thanks
Mona
[Grapecity]


pagates said:
Hi All,

I am attempting to write values to the registry, but get an
UnauthorizedAccessException when I try to do a SetValue on an existing key.
However, I can create the key, with a value, when it does not exist.

Here is a snippet of the code:
Dim RegKey As RegistryKey
RegKey = Registry.LocalMachine.OpenSubKey(BaseKey & "\" &
SubKey, False)
If (RegKey Is Nothing) Then
RegKey = Registry.LocalMachine.CreateSubKey(BaseKey &
"\" & SubKey)
End If
Try
RegKey.SetValue("NumberRuns",
Tasks(I).NumberRuns.ToString())
RegKey.SetValue("TotalSeconds",
Tasks(I).TotalSeconds.ToString())
Catch ex As Exception
MessageBox.Show("Exception: " & ex.Message)
Finally
RegKey.Close()
End Try

What do I need to do to be able to update this key?

Thanks,
pagates
 
C

Chris Dunaway

Well, what is the programmer to do when the parameters forget to set
themselves?? :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top