Search code examples
c#winformsregistry

Delete Folder from Registry - Permission Issues


I am trying to delete one folder from registry. Let's say I want to delete the folder

Software\TeamViewer

I have written code but it is gives an exception "you can't write". I guess its some kind of problem with the Permission & access rights.

string keyapath = @"Software\TeamViewer";
RegistryKey regKeyAppRoot = Registry.CurrentUser.OpenSubKey(keyapath);
regKeyAppRoot.DeleteSubKeyTree(keyapath);

How do I give permission to my software to delete folders from registry?

EDIT: I have admin rights of my system. Do I still need to exclusively assign rights to the application through my code?


Solution

  • The OpenSubKey method with one parameter opens the key for reading. Use other variant of OpenSubKey method:

    OpenSubKey(String, Boolean) -- Pass true for a second parameter to open the key with generic write access

    OpenSubKey(String, RegistryKeyPermissionCheck) -- Allows some precise control over the permission chacking for subkeys

    OpenSubKey(String, RegistryKeyPermissionCheck, RegistryRights) -- As above, but you can exactly specify needed rights.

    See MSDN for details.