Question Can't write to registry

davidl

New member
Joined
Jan 11, 2010
Messages
1
Programming Experience
5-10
The following code causes an error but should work. Any ideas? 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.


VB.NET:
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\Connection Tools")
If tmp2 Is Nothing Then
   tmp.CreateSubKey("SOFTWARE\Connection Tools")
   tmp2 = tmp.OpenSubKey("SOFTWARE\Connection Tools")
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
 
Last edited:

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,583
Location
Norway
Programming Experience
10+
Top Bottom