How to get past SecurityException accessing Registry?

Nicholas Piazza 541 Reputation points
2021-01-06T22:29:33.673+00:00

In the Microsoft documentation for System.Console (https://learn.microsoft.com/en-us/dotnet/api/system.console?view=net-5.0#examples) there is an example related to "font linking". One part of that example is shown in the code snippet

            RegistryKey key =   
                Registry.LocalMachine.OpenSubKey   
                    (@"Software\Microsoft\Windows NT\CurrentVersion\FontLink\SystemLink",  
                     true);  

However, when I run the example, I get a SecurityException as shown below:
54027-accesserror.png

How do I give myself permission to access the Registry from a C# program?

Developer technologies | C#
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. RLWA32 49,541 Reputation points
    2021-01-07T00:28:14.58+00:00

    Write access to the HKLM portion of the registry usually requires that a process is running with elevated privileges (as Administrator). By passing "true" to OpenSubkey the sample code is requesting write access.

    1 person found this answer helpful.

  2. RLWA32 49,541 Reputation points
    2021-01-18T22:07:35.993+00:00

    If you don't want to right-click and select "run as Administrator" then you can manifest your application to "requireAdministrator".

    0 comments No comments

  3. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2021-01-18T22:22:04.35+00:00

    Have you tried adding a manifest file and work with the following by uncommenting.

    57951-11111111111.png

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.