By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,058 Members | 1,209 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,058 IT Pros & Developers. It's quick & easy.

Writing to Registry: Unauthorized Access Exception

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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" <pa*****@discussions.microsoft.com> wrote in message
news:D5**********************************@microsof t.com...
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

Nov 21 '05 #2

P: n/a
Hi Mona,

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

Thanks again,
PAGates

"Mona" wrote:
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" <pa*****@discussions.microsoft.com> wrote in message
news:D5**********************************@microsof t.com...
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


Nov 21 '05 #3

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

Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.