【发布时间】:2011-09-16 12:25:02
【问题描述】:
我希望就一个我似乎无法解决的错误获得一些帮助。
一点背景知识 - 我们有一个使用注册表的大型 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()
【问题讨论】:
-
发布异常的堆栈跟踪。
-
尝试运行代码(同时使用 .NET 4 和 3.5),它工作得很好。也许你使用了无效字符?请参考这个问题:stackoverflow.com/questions/1013473/…
-
@George,请查看我对下面答案的编辑,我添加了另一种可能的解决方案(关于 UAC)。
-
作为新用户需要了解的一些事项。首先,请不要在您的帖子中添加标语。其次,答案应该是answers,而不是回复。如果您需要为您的问题添加更多详细信息,edit it。如果您需要回复答案,您可以发表评论(在此处以及达到 50 代表后的任何地方)。如需更多信息,请查看faq 和Meta Stack Overflow。谢谢。
-
您不需要使用
OpenSubKey和CreateSubKey。只需CreateSubKey将在密钥丢失时创建密钥,如果密钥已存在则打开以写入。