none
Registry error made me crazy " Requested registry access is not allowed"

    Question

  • dear everyone i made a windows application using c#, in my application i have to write values in registry.. the problem exists in limited users (not administrator) accounts this error exception appear " Requested registry access is not allowed"

    tried to solve this issue by 2 methods

    method 1:

    i put in my manifest file the below code

    <?xml version="1.0" encoding="utf-8"?>
    <asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
      <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
        <security>
          <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
            <!-- UAC Manifest Options
                If you want to change the Windows User Account Control level replace the 
                requestedExecutionLevel node with one of the following.
    
            <requestedExecutionLevel  level="asInvoker" uiAccess="false" />
            <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
            <requestedExecutionLevel  level="highestAvailable" uiAccess="false" />
    
                If you want to utilize File and Registry Virtualization for backward 
                compatibility then delete the requestedExecutionLevel node.
            -->
            <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
          </requestedPrivileges>
        </security>
      </trustInfo>
    </asmv1:assembly>
    

    Method 2:

    i tried to make the registry has full control for users so i can write into registry regardless of user type using this code

                
                string user = Environment.UserDomainName + "\\" + Environment.UserName;
    
                RegistrySecurity rs = new RegistrySecurity();
    
                // Allow the current user to read and delete the key. 
                //
                rs.AddAccessRule(new RegistryAccessRule(user,
                    RegistryRights.ReadKey | RegistryRights.WriteKey | RegistryRights.Delete,
                    InheritanceFlags.None,
                    PropagationFlags.None,
                    AccessControlType.Allow));

    it solve the error in many computers but there are many other computer has the same error.

    i want to solve this error but i can not make it in all versions of windows.

    please advice as soon as possible

    Wednesday, October 30, 2013 1:38 PM

Answers

  • During installation your app is generally being run as an admin.  Each install program is different but they should allow you to specify permissions on a key when the key is created.  In .NET you would use SetAccessControl.  But that isn't available during installation.  You have to have (elevated) admin privileges in order to change the permissions on a registry key in HKLM.

    What you're asking to do is in .NET code.  This indicates to me that you don't have a setup program.  Therefore you must run your app as an admin (at least once).  A normal user will not be able to do that.  Some apps require that you run them as admin the first time so they can make such changes.  You might need to go that route.

    For more info on using access control in .NET refer to MSDN.  You can also read this post that I made years ago on how to configure inheritance and whatnot.

    Michael Taylor
    http://msmvps.com/blogs/p3net

    Thursday, October 31, 2013 2:10 PM

All replies

  • The registry is locked down for most users (especially HKLM).  This is the correct behavior and you should not try to change that.  If everyone has registry write access then it won't be long before users will irrevocably damage their Win machine.

    You can, if you need to, change the rights on your local subkey under HKLM to allow users to write to it but that should be done at install time.  Ideally though you'll store per-user data in HKCU instead.

    If you just need to read from the registry then you should make sure you open the key for read access only.  Most keys can be read by normal users.

    The manifest won't help a normal user.  It merely forces elevation when being run by someone that can be elevated.

    Michael Taylor
    http://msmvps.com/blogs/p3net

    Wednesday, October 30, 2013 2:30 PM
  • You can, if you need to, change the rights on your local subkey under HKLM to allow users to write to it but that should be done at install time.

    how to allow users to write in hklm during setup... i tried many codes but it did not work for me.. please reply as fast as possible
    Thursday, October 31, 2013 7:29 AM
  • The registry is locked down for most users (especially HKLM).  This is the correct behavior and you should not try to change that.  If everyone has registry write access then it won't be long before users will irrevocably damage their Win machine.

    You can, if you need to, change the rights on your local subkey under HKLM to allow users to write to it but that should be done at install time.  Ideally though you'll store per-user data in HKCU instead.

    If you just need to read from the registry then you should make sure you open the key for read access only.  Most keys can be read by normal users.

    The manifest won't help a normal user.  It merely forces elevation when being run by someone that can be elevated.

    Michael Taylor
    http://msmvps.com/blogs/p3net


    location of check box : regedit --> any load hive at h user hive --> right click and select permissions --> click advance tab and then

    at bootom two check box is there.and i want to select second one programmatically .

    please guide me.

    Thursday, October 31, 2013 8:19 AM
  • During installation your app is generally being run as an admin.  Each install program is different but they should allow you to specify permissions on a key when the key is created.  In .NET you would use SetAccessControl.  But that isn't available during installation.  You have to have (elevated) admin privileges in order to change the permissions on a registry key in HKLM.

    What you're asking to do is in .NET code.  This indicates to me that you don't have a setup program.  Therefore you must run your app as an admin (at least once).  A normal user will not be able to do that.  Some apps require that you run them as admin the first time so they can make such changes.  You might need to go that route.

    For more info on using access control in .NET refer to MSDN.  You can also read this post that I made years ago on how to configure inheritance and whatnot.

    Michael Taylor
    http://msmvps.com/blogs/p3net

    Thursday, October 31, 2013 2:10 PM