locked
Access to the registry key is denied. Seems I'm having RegistrySecurity issues.

    Question

  • Hi.  I'm trying to add a key to the registry and I can't seem to get past the security.  What am I doing wrong here? Am I missing something? Here is the code that I am using:

    public Form1()
     {
            InitializeComponent();
            RegistryKey rKey;
            rKey = Registry.LocalMachine.OpenSubKey  ("\\SOFTWARE\\Wow6432Node\\Alpha\\DB");
    if (rKey == null)
            {
      string szUser = Environment.UserDomainName + "\\" + Environment.UserName;
      RegistrySecurity rSec = new RegistrySecurity();
                    rSec.AddAccessRule(new RegistryAccessRule(szUser,
                    RegistryRights.ReadKey | RegistryRights.WriteKey,
                    InheritanceFlags.None,
                    PropagationFlags.None,
                    AccessControlType.Allow));

      rKey = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Wow6432Node\\Alpha\\DB",
       RegistryKeyPermissionCheck.Default, rSec);
            }
     }

    Thanks for looking



    • Edited by bmkiss67 Wednesday, September 19, 2012 9:08 PM
    Wednesday, September 19, 2012 9:07 PM

Answers

  • Thanks for everyones replies.

    Yes, I am running VS as admin.

    But one of the other programmers has just changed an aspect of this by giving everyone permission on that set of keys that I was working on.  It would seem that I no longer need to worry about the permissions issues I was having.  I also found, after doing a lot of searching, a different way of opening the keys that seems to work much better, as least for me.  It goes like this:

    RegistryKey localMachine;
    localMachine = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry32);
    rKey = localMachine.OpenSubKey(@"SOFTWARE\Alpha\DB");

    That view oprion seems to make things flow better for me.

    Anyway, thanks for everyone's help. Hopefully they don't change the permission idea around again.

    Monday, September 24, 2012 1:59 PM

All replies

  • May be the problem is related to registry redirection and reflection(happens on 64 bit OS)

    please check-out below links

    http://msdn.microsoft.com/en-us/library/windows/desktop/aa384235(v=vs.85).aspx

    http://msdn.microsoft.com/en-us/library/windows/desktop/aa384232(v=vs.85).aspx


    Regards,
    Ahmed Ibrahim
    SQL Server Setup Team
    My Blog
    This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to click "Mark as Answer" and "Vote as Helpful" on posts that help you.
    This can be beneficial to other community members reading the thread.
    View Ahmed Ibrahim's profile on LinkedIn

    Wednesday, September 19, 2012 11:58 PM
  • Thanks for your reply, but I don't think that redirection or reflection has anything to do with this issue.  I don't understand why that could isn't do what I expected it to do.
    Friday, September 21, 2012 4:35 PM
  • Could you check whether you have the required permissions - "ReadPermissions | ChangePermissions | TakeOwnership" -  on the registry key path you wish to add the ACLs? (TakeOwnership may not be required though).

    As you are creating subkey for the same node, don't you need the CreateSubkey permission too?

    Side-note: It is a preferred way to validate your code first with a temporary testing RegistryKey path where you have FullControl and Ownership. After you confirmed the correctness of code, you can proceed with the production code on the required path with required rights.

    Sorry-note: You might know these information already because you have used RegistryRights in your code properly. Sorry if I have repeated what you know already.


    Friday, September 21, 2012 7:19 PM
  • Are you running VS as Admin ??

    Digital Forensic Software Developer
    CCS LABS Digital Forensic Software
    Mark as Answer or Vote up if useful thank you!

    Saturday, September 22, 2012 3:34 AM
  • Hi,

     HKey_Local_Machine

    Try opening each registry key individually like this

        Microsoft.Win32.RegistryKey key1 = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE");
    Microsoft.Win32.RegistryKey key2 = key1.OpenSubKey(@"ADM");

    Thanks & Regards

     NVM Prasad


    Thanks & Regards NVM Prasad * prasadforum@hotmail.com Skype ID: prasad.nvm

    Saturday, September 22, 2012 4:41 AM
  • Thanks for everyones replies.

    Yes, I am running VS as admin.

    But one of the other programmers has just changed an aspect of this by giving everyone permission on that set of keys that I was working on.  It would seem that I no longer need to worry about the permissions issues I was having.  I also found, after doing a lot of searching, a different way of opening the keys that seems to work much better, as least for me.  It goes like this:

    RegistryKey localMachine;
    localMachine = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry32);
    rKey = localMachine.OpenSubKey(@"SOFTWARE\Alpha\DB");

    That view oprion seems to make things flow better for me.

    Anyway, thanks for everyone's help. Hopefully they don't change the permission idea around again.

    Monday, September 24, 2012 1:59 PM