Click here to Skip to main content
15,899,935 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The following code causes an error but should work. Why doesn't it work? The same code (originally in REALbasic) does work so the currentuser (also an admin on the pc) does have access to write to the registry.

Public Sub SavePreference(ByVal pref As String, ByVal value As String)
Dim tmp As RegistryKey = Registry.CurrentUser
Dim tmp2 As RegistryKey
tmp2 = tmp.OpenSubKey("SOFTWARE\example")
If tmp2 Is Nothing Then
   tmp.CreateSubKey("SOFTWARE\example")
   tmp2 = tmp.OpenSubKey("SOFTWARE\example")
End If
If tmp2 IsNot Nothing Then tmp2.SetValue(pref, LCase(value))
End Sub


A first chance exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll

err> Cannot write to the registry key.
stack>
at System.ThrowHelper.ThrowUnauthorizedAccessException(ExceptionResource resource)
at Microsoft.Win32.RegistryKey.EnsureWriteable()
at Microsoft.Win32.RegistryKey.SetValue(String name, Object value, RegistryValueKind valueKind)
at Microsoft.Win32.RegistryKey.SetValue(String name, Object value)
at ConnectionTools.mMiscFunction.SavePreference(String pref, String value) in mMiscFunction.vb:line 92
Posted
Updated 11-Jan-10 8:04am
v3

You're using the wrong overload of Registry.OpenSubkey[^]. Registry.OpenSubkey(String) opens as read-only. Use RegistryKey.OpenSubKey(String, Boolean) instead. ie:

Public Sub SavePreference(ByVal pref As String, ByVal value As String)     
    Dim tmp As RegistryKey = Registry.CurrentUser
    Dim tmp2 As RegistryKey
    tmp2 = tmp.OpenSubKey("SOFTWARE\example", True)
    If tmp2 Is Nothing Then   
        tmp.CreateSubKey("SOFTWARE\example")   
        tmp2 = tmp.OpenSubKey("SOFTWARE\example", True)
    End If
    If tmp2 IsNot Nothing Then tmp2.SetValue(pref, LCase(value))
End Sub
 
Share this answer
 
Well, looks like the currently logged in user doesn't have rights to modify the registry. What's the question?
 
Share this answer
 
Why doesn't it work? The same code (modified for REALbasic) does work so the currentuser (also an admin on the pc) does have access to write to the registry.
 
Share this answer
 
The point is, it works in another language so i do have access to write to the registry so VB.NET should not be giving an access error.
 
Share this answer
 
That exception can be thrown if the registry key is read-only as well. REALbasic might automatically handle that for you.
 
Share this answer
 
It's not read only because i can change the key. I am testing it with tracking the position of a form on the screen. It writes the top and left coords to the registry. If i run the REALbasic app it updates, the VB.net app just throws the error.
 
Share this answer
 
I dunno. I've never heard of "REALbasic".
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900