开发者

vb.net RegistryKey.CreateSubKey error help

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 
   Micro开发者_开发知识库soft.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()


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜