Cannot write to the registry key

esteuart

Member
Joined
May 6, 2003
Messages
11
I need my stand-alone application to both read and write to the registry. I have no problems reading from the registry, but whenever I try to save my keys to the registry I get the following error:

An unhandled exception of type System.UnauthorizedAccessException occurred in mscorlib.dll
Additional information: Cannot write to the registry key.


I assume it has something to do with security permissions - I dont know. If it is a security issue, how do I give my application permission to update the registry?

Here is the code
Code:
Private key As Microsoft.Win32.RegistryKey
Private Const KEY_LOCATION = "Software\Company\Product"

Public Sub New()
        key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(KEY_LOCATION)
End Sub

Private Sub frmService_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        tbBroadcast.Text = key.GetValue("Broadcast", "")
        tbBgPath.Text = key.GetValue("InstallLocation", "")
        nudTimer.Value = key.GetValue("TimerLength", 10)
End Sub

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        key.SetValue("Broadcast", tbBroadcast.Text)
        key.SetValue("InstallLocation", tbBgPath.Text)
        key.SetValue("TimerLength", nudTimer.Value)
End Sub

The program dies on key.SetValue("Broadcast", tbBroadcast.Text).

Please help!
 
Try this


[VB]
Public Sub New()
key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(KEY_LOCATION, TRUE)
End Sub
[/VB]

Note the True, which sets, if the registry is read-only or read-write (default is False = read-only).
 
Back
Top