vb.netRegistryKey.CreateSubKey错误帮助

发布于 2024-12-05 03:01:02 字数 3791 浏览 2 评论 0原文

我希望得到一些帮助来解决我似乎无法解决的错误。

一些背景知识 - 我们有一个使用注册表的大型 Windows 应用程序(最初在 .NET v1.1 中写回,已升级到 v3.5)。我正在尝试使其兼容 64 位,并且快完成了。无论如何,我们还有一个小型 .net 实用程序应用程序,它是为系统管理员编写的,负责维护该应用程序以编辑/更改注册表值,因为它们是加密的,不能仅通过 Regedit 进行更改。我遇到的错误是在实用程序应用程序代码中。

错误:“指定的RegistryKeyPermissionCheck值无效。参数名称:模式”

当实用程序代码尝试创建非现有的子项,如下所示:

Dim regKey As RegistryKey
regKey = Registry.LocalMachine.OpenSubKey("Software\[APPLICATION]\[SUBKEY]\", True)

If regKey Is Nothing Then
   Dim tempKey As RegistryKey
   tempKey = Registry.LocalMachine.OpenSubKey("SOFTWARE", True)
   tempKey.CreateSubKey("[APPLICATION]\[SUBKEY]\")
   tempKey.Close()
   regKey = Registry.LocalMachine.OpenSubKey("[APPLICATION]\[SUBKEY]\", True)
End If

当代码到达 CreateSubKey 行时,它会抛出错误...是否存在此错误?

顺便说一句,代码可以很好地写入、读取和设置现有的注册表项值。如果您有任何疑问,请告诉我。


这是堆栈跟踪:

System.ArgumentException was unhandled
Message="The specified RegistryKeyPermissionCheck value is invalid. Parameter name: mode"
ParamName="mode"
Source="mscorlib"
StackTrace:
   at Microsoft.Win32.RegistryKey.ValidateKeyMode(RegistryKeyPermissionCheck mode)
   at Microsoft.Win32.RegistryKey.CreateSubKey(String subkey, 
   RegistryKeyPermissionCheck permissionCheck, RegistrySecurity registrySecurity)    
   at PolarisRegistryEditor.PolarisKeys.SaveRegistryvalues(RegistryKey reg) in C:\apps
   \Visual Studio 2008\Projects\PolarisRegistryEditor\PolarisRegistryEditor
   \PolarisKeys.vb:line 321
   at PolarisRegistryEditor.PolarisKeys.Save() in C:\apps\Visual Studio 2008\Projects
   \PolarisRegistryEditor\PolarisRegistryEditor\PolarisKeys.vb:line 278    
   at PolarisRegistryEditor.Form1.btnSet_Click(Object sender, EventArgs e) in 
   C:\apps\Visual Studio 2008\Projects\PolarisRegistryEditor\PolarisRegistryEditor
   \Form1.vb:line 36    
   at System.Windows.Forms.Control.OnClick(EventArgs e)    at
   System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)    at 
   System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32
   clicks)    at System.Windows.Forms.Control.WndProc(Message& m)    at
   System.Windows.Forms.ButtonBase.WndProc(Message& m)    at 
   System.Windows.Forms.Button.WndProc(Message& m)    at 
   System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)    at 
   System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, 
   IntPtr wparam, IntPtr lparam)    at 
   System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)    at 
   System.Windows.Forms.Application.ComponentManager.System.Windows.
   Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 
   dwComponentID, Int32 reason, Int32 pvLoopData)    at 
   System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 
   reason, ApplicationContext context)    at 
   System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, 
   ApplicationContext context)    at 
   Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
   at  Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.
   DoApplicationModel()    at 
   Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.
   Run(String[] commandLine)    at
   PolarisRegistryEditor.My.MyApplication.Main(String[] Args) in 
   17d14f5c-a337-4978-8281-53493378c1071.vb:line 81    at 
   System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)    at 
   Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()    at 
   System.Threading.ExecutionContext.Run(ExecutionContext executionContext, 
   ContextCallback callback, Object state)    at 
   System.Threading.ThreadHelper.ThreadStart()

I was hoping to get some help with an error I can't seem to get around.

A little background - we have a large windows application that uses the registry (originally written back in .NET v1.1 and has been upgraded to v3.5). I'm trying to get it 64-bit compliant and am almost done. Anyhow, we also have a small .net utility application that was written for the system admins that maintain the application to edit/change the registry values since they are encrypted and can't just be changed via Regedit. The error I'm getting is in the utility application code.

Error: "The specified RegistryKeyPermissionCheck value is invalid. Parameter name: mode"

The error pops up when the utility code attempts to create a non-existant subkey, like so:

Dim regKey As RegistryKey
regKey = Registry.LocalMachine.OpenSubKey("Software\[APPLICATION]\[SUBKEY]\", True)

If regKey Is Nothing Then
   Dim tempKey As RegistryKey
   tempKey = Registry.LocalMachine.OpenSubKey("SOFTWARE", True)
   tempKey.CreateSubKey("[APPLICATION]\[SUBKEY]\")
   tempKey.Close()
   regKey = Registry.LocalMachine.OpenSubKey("[APPLICATION]\[SUBKEY]\", True)
End If

When the code hits the CreateSubKey line it throws the error...is there anyway around this error?

BTW, the code can write to, read from and set existing registry key values just fine. Let me know if you have any questions.


Here is the stack trace:

System.ArgumentException was unhandled
Message="The specified RegistryKeyPermissionCheck value is invalid. Parameter name: mode"
ParamName="mode"
Source="mscorlib"
StackTrace:
   at Microsoft.Win32.RegistryKey.ValidateKeyMode(RegistryKeyPermissionCheck mode)
   at Microsoft.Win32.RegistryKey.CreateSubKey(String subkey, 
   RegistryKeyPermissionCheck permissionCheck, RegistrySecurity registrySecurity)    
   at PolarisRegistryEditor.PolarisKeys.SaveRegistryvalues(RegistryKey reg) in C:\apps
   \Visual Studio 2008\Projects\PolarisRegistryEditor\PolarisRegistryEditor
   \PolarisKeys.vb:line 321
   at PolarisRegistryEditor.PolarisKeys.Save() in C:\apps\Visual Studio 2008\Projects
   \PolarisRegistryEditor\PolarisRegistryEditor\PolarisKeys.vb:line 278    
   at PolarisRegistryEditor.Form1.btnSet_Click(Object sender, EventArgs e) in 
   C:\apps\Visual Studio 2008\Projects\PolarisRegistryEditor\PolarisRegistryEditor
   \Form1.vb:line 36    
   at System.Windows.Forms.Control.OnClick(EventArgs e)    at
   System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)    at 
   System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32
   clicks)    at System.Windows.Forms.Control.WndProc(Message& m)    at
   System.Windows.Forms.ButtonBase.WndProc(Message& m)    at 
   System.Windows.Forms.Button.WndProc(Message& m)    at 
   System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)    at 
   System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, 
   IntPtr wparam, IntPtr lparam)    at 
   System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)    at 
   System.Windows.Forms.Application.ComponentManager.System.Windows.
   Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 
   dwComponentID, Int32 reason, Int32 pvLoopData)    at 
   System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 
   reason, ApplicationContext context)    at 
   System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, 
   ApplicationContext context)    at 
   Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
   at  Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.
   DoApplicationModel()    at 
   Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.
   Run(String[] commandLine)    at
   PolarisRegistryEditor.My.MyApplication.Main(String[] Args) in 
   17d14f5c-a337-4978-8281-53493378c1071.vb:line 81    at 
   System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)    at 
   Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()    at 
   System.Threading.ExecutionContext.Run(ExecutionContext executionContext, 
   ContextCallback callback, Object state)    at 
   System.Threading.ThreadHelper.ThreadStart()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逆蝶 2024-12-12 03:01:02

从错误来看,您似乎没有足够的权限来创建注册表项。

也许在您的计算机上只有管理员才能修改注册表的内容。
或者:

  1. 要求设置启动密钥的代码以管理员权限运行
  2. 在尝试创建密钥之前,为登录用户分配注册表项的权限。

这个链接应该有帮助:
http://msdn.microsoft.com/en -us/library/microsoft.win32.registrykey.setaccesscontrol.aspx#Y100

编辑:

如果您确定您有足够的权限写入注册表,那么我能想到的最后一件事是UAC

请尝试禁用该用户帐户控制并查看是否可以解决问题。

From the error it seems that you have not enough privileges to create the registry key.

Maybe on your machine only Administrators can modify the contents of the registry.
Either:

  1. Require that the code that sets your startup key be run with Administrator privileges
  2. Assign permissions on the registry key for the logged in user, before you try to create a key.

this link should help:
http://msdn.microsoft.com/en-us/library/microsoft.win32.registrykey.setaccesscontrol.aspx#Y100

Edit:

If you art sure you have sufficient privileges to write to the registry, then the last thing that I can think of is UAC.

Please try to disable the User Account Control and see if it solves the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文