无法在currentuser中写入注册表 [英] can't write to registry in currentuser

查看:203
本文介绍了无法在currentuser中写入注册表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码会导致错误,但应该可以工作.为什么不起作用?相同的代码(最初在REALbasic中)起作用了,因此currentuser(也是PC上的管理员)确实有权写入注册表.

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



mscorlib.dll中发生了类型为"System.UnauthorizedAccessException"的第一次机会异常

err>无法写入注册表项.
堆栈>
在System.ThrowHelper.ThrowUnauthorizedAccessException(ExceptionResource资源)处
在Microsoft.Win32.RegistryKey.EnsureWriteable()
在Microsoft.Win32.RegistryKey.SetValue(字符串名称,对象值,RegistryValueKind valueKind)
在Microsoft.Win32.RegistryKey.SetValue(字符串名称,对象值)
在mMiscFunction.vb中的ConnectionTools.mMiscFunction.SavePreference(字符串首选项,字符串值)处:线92



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

推荐答案

您正在使用Registry.OpenSubkey [
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


好吧,看起来当前登录的用户没有修改注册表的权限.有什么问题?
Well, looks like the currently logged in user doesn''t have rights to modify the registry. What''s the question?


为什么不起作用?相同的代码(为REALbasic修改)确实起作用,因此当前用户(也是PC上的管理员)有权访问注册表.
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.


这篇关于无法在currentuser中写入注册表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆