none
System.UnauthorizedAccessException when creating registry keys - Please help

    Question

  • Hi,

    I am trying to write into the registry from my application and I get the below exception:

    System.UnauthorizedAccessException was unhandled
      Message="Cannot write to the registry key."
      Source="mscorlib"
      StackTrace:
           at System.ThrowHelper.ThrowUnauthorizedAccessException(ExceptionResource resource)
           at Microsoft.Win32.RegistryKey.CreateSubKey(String subkey, RegistryKeyPermissionCheck permissionCheck, RegistrySecurity registrySecurity)
           at Microsoft.Win32.RegistryKey.CreateSubKey(String subkey)
           at ReadFileNames.Form1.Form1_Load(Object sender, EventArgs e) in D:\Bancassurance\OFX Specs\OFX2.1.1\ReadFileNames\Form1.cs:line 35
           at System.Windows.Forms.Form.OnLoad(EventArgs e)
           at System.Windows.Forms.Form.OnCreateControl()
           at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
           at System.Windows.Forms.Control.CreateControl()
           at System.Windows.Forms.Control.WmShowWindow(Message& m)
           at System.Windows.Forms.Control.WndProc(Message& m)
           at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
           at System.Windows.Forms.ContainerControl.WndProc(Message& m)
           at System.Windows.Forms.Form.WmShowWindow(Message& m)
           at System.Windows.Forms.Form.WndProc(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(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.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
           at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
           at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
           at System.Windows.Forms.Control.set_Visible(Boolean value)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.Run(Form mainForm)
           at ReadFileNames.Program.Main() in D:\Bancassurance\OFX Specs\OFX2.1.1\ReadFileNames\Program.cs:line 17
           at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
           at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()

    I am logged in as an administrator user and also able to create registry keys through regedit. Please help fix this issue.

    Thanks.

    Tuesday, February 20, 2007 10:50 AM

Answers

  •  *Ram* wrote:
    You might be using the wrong overload of the CreateSubKey method. In order to be able to write to the registry, use the overload of CreateSubKey method that takes the bool parameter. Pass 'true' as the parameter to have write access to the registry.

    There is no Overload of CreateSubKey which takes a bool unlike OpenSubKey, A key opened or created with CreateSubKey opens in write mode.

    Best Regards,

    Rizwan aka RizwanSharp

    Friday, February 23, 2007 2:21 PM

All replies

  • post the code
    Tuesday, February 20, 2007 8:05 PM
  • You might be using the wrong overload of the CreateSubKey method. In order to be able to write to the registry, use the overload of CreateSubKey method that takes the bool parameter. Pass 'true' as the parameter to have write access to the registry.
    Thursday, February 22, 2007 5:09 AM
  • u must not be having proper rights to write to folder

    Right click on tat folder n set its permissionsss

    Friday, February 23, 2007 6:41 AM
  •  *Ram* wrote:
    You might be using the wrong overload of the CreateSubKey method. In order to be able to write to the registry, use the overload of CreateSubKey method that takes the bool parameter. Pass 'true' as the parameter to have write access to the registry.

    There is no Overload of CreateSubKey which takes a bool unlike OpenSubKey, A key opened or created with CreateSubKey opens in write mode.

    Best Regards,

    Rizwan aka RizwanSharp

    Friday, February 23, 2007 2:21 PM
  • Hi Rizwan,

    Yes, I was not using the right overload of the OpenSubkey() method.

    Thanks for the help.

    Wednesday, February 28, 2007 7:02 AM
  •   

    If your getting the following error

    A first chance exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll

    on Vista while trying to create a registry key, even though you are logon as administrator.
    You have to close the development environment and restart it by right clicking the shortcut to the ide and selecting run as Administrator.

    And reopen your project , now you will be able to create the key.

    Hope this was helpfull


    Wednesday, December 03, 2008 4:38 PM
  • Thanks Rizwan,

         Here it is four years after this post, and your answer helped me out!  Thanks.

    Tuesday, March 22, 2011 2:39 AM