Writing to Registry: Unauthorized Access Exception

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
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]
 
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
 
Well, what is the programmer to do when the parameters forget to set
themselves?? :)
 
Back
Top