Does anyone know how to get access to a Registry key and delete all Subkeys? RRS feed

  • Question

  • Hi developers,

    I need to gain access to this Registry key and delete all sub keys, but I keep getting a permission error

    This is the actual Registry Key that I want to remove ll the sub keys from:

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles

    This is the code that is throwing an error:

    If I use this code, I get a permissions Error

    RegistryKey aRegkey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\NetworkList", true);

    If I add the profiles Key, i Get a Null reference:

    RegistryKey aRegkey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\NetworkList\\Profiles", true);

    I think I have to do something like this to get security permissions . . .

    var security = aRegkey.GetAccessControl();
    var rules = security.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount));

    Any Help would be greatly appreciated :))


    Sunday, August 12, 2018 5:45 PM

All replies

  • Your application must be running with elevated privilege (i.e., as Adminstrator) in order to read/write in the HKLM portion of the registry.

    Also, since you are using @ for verbatim string literals single backslach characters in the registry path are sufficient.

    But why do you want to manually delete these registry keys instead of managing Networks through the user interface?
    • Edited by RLWA32 Sunday, August 12, 2018 9:02 PM
    Sunday, August 12, 2018 8:59 PM
  • Hi RLWA32,

    Thank you for responding, I appreciate that and for the tip regarding escaping the backslash . . 

    I am writing a very specific utility for someone and they need to remove all traces of WiFi networks when their PC shuts down, therefore, I need to delete all sub-keys of that Profiles Registry key . . .


    Sunday, August 12, 2018 9:41 PM
  • Be careful.  Wired networks can also be present in the Profiles sub-key.
    Sunday, August 12, 2018 10:02 PM
  • I think I have worked out it is actually a premissions issue on the ProfileList registry key
    In my code, I can access this key and return a list of all Sub-keys (including ProfileList)
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion 
    C# will not, for some reason, see this key at all, even though it is listed as a sub-key when I access the parent key in C#
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList


    Tuesday, August 14, 2018 12:31 PM
  • That registry key has nothing to do with network profiles. That key is for the registered user profiles on the computer. You do not want to touch that if you want that computer to keep working.

    The subkeys are set up the way they are so it requires extra steps to do anything for the very reason that these keys are such important keys.

    This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    Tuesday, August 14, 2018 12:47 PM
  • Thanks for all the answers, I got this resolved . . .


    Saturday, August 18, 2018 12:37 PM
  • Hi DaveM121,

    I am glad to know that you resolve the issue, could you please share your solution and mark it as answer, it will be beneficial to other communities who have the similar issue.

    Best regards,


    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

    Thursday, September 6, 2018 8:25 AM