none
Cannot Write To Registry Key HKEY_LOCAL_MACHINE \\SOFTWARE\\ In Windows 7 with Administrator Powered User Using Code

    Question

  • In Windows 7, using visual studio 2008,

    When i am using code :

    RegistryKey rk=Registry.LocalMachine.CreateSubKey("Software\\subkey\\subkey");

    I am getting error: Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\subkey\subkey' is denied.

    I am not able to create subkey in localmachine\software\.. through code,when i a administrator powered user.

    But this code will work when iam woking it from exact Administrator account and will not work from admin powered account.

    Any idea to solve this issue.

    Thanks in advance.

     

    Tuesday, February 01, 2011 6:11 AM

All replies

  • With UAC you get a filtered token which represents standard user rights, so you can't write to HKEY_LOCAL_MACHINE.

    You have to add a new manifest to the app:

     

      <security>
    <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
    <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
    </requestedPrivileges>
    </security>
    <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
    <application>
    <!-- UAC supportedOS Options
    {e2011457-1546-43c5-a5fe-008deee3d3f0} = Vista
    {35138b9a-5d96-4fbd-8e2d-a2440225f93a} = Windows 7
    -->
    <supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"/>
    <supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>
    </application>
    </compatibility>
    Now you get a UAC prompt each time you run the application. When you accept the prompt you get the full admin token for the program and you can set the value.

    André

     


    "A programmer is just a tool which converts caffeine into code" CLIP- Stellvertreter http://www.winvistaside.de/
    Tuesday, February 01, 2011 1:58 PM
  • Thanks for your replay. By implementing above procedure, when i am calling some files in system 32 in user account, the UAC prompt will come and it is working.But when iam trying to create registry subkey through code then i am getting error:Access denied.
    Friday, February 04, 2011 11:51 AM