none
Registry.LocalMachine with OpenSubKey yields 'System.Security.SecurityException' RRS feed

  • Question

  • I am trying to run the following:

    RegistryKey rk = Registry.LocalMachine;
    RegistryKey softwareKey = rk.OpenSubKey("Software",
                                               RegistryKeyPermissionCheck.ReadWriteSubTree);

    in the debugger.  My account has administrative privileges, but I get exception 'System.Security.SecurityException' on the second line of code.

    Is there no way to run this code in the debugger?  Or am I doing something stupid?

    I am running Windows 7 Pro, Visual Studio 2010.

    Thanks in advance,
    Michael Bate

    Saturday, February 9, 2019 4:29 AM

All replies

  • Add a Manifest file with :

    <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

    Saturday, February 9, 2019 9:35 AM
  • E;levated privilege is needed for read/write access to HKLM. 

    With UAC enabled processes started by members of the Administrators group run with Limited user privileges unless they are started with "run as Administrator" or are manifested to "requireAdministrator", both of which cause the new process to start with elevated privileges.

    Start Visual Studio with "run as Administrator" so that it is elevated.  You should now be able to debug your code

    Saturday, February 9, 2019 11:15 AM
  • Thank you!

    But when I make the change you requested I get the error message:

    Error    42    ClickOnce does not support the request execution level 'requireAdministrator'.

    I've never worked with manifests or requestedExecutionLevel before.  I have worked with the registry, but always with Current User.  Apparently LocalMachine needs a higher level of administrative privilege and I am concerned that my clients (actually the customers of my clients) may not want to work with this problem.

    I am not sure what "ClickOnce" refers to.  Do you have any suggestions?

    Sunday, February 10, 2019 10:43 PM
  • Hi MichaelBate,

    Thank you for posting here.

    For your question, run as admin could make the code have enough permission. 

    >>Error    42    ClickOnce does not support the request execution level 'requireAdministrator'.

    Based on my test, I reproduce you error.

    When you use 'requireAdministrator', you need to tick the Enable ClickOnce security settings to enable .manifest file to set the 'requireAdministrator'. After that, please uncheck the Enable ClickOnce security settings. Everything will be okay.

    Or you just the run the Visual Studio as Admin.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Stanly Fan Tuesday, February 12, 2019 6:24 AM
    Monday, February 11, 2019 7:55 AM
    Moderator