RegistryKey.DeleteSubKeyTree throws an UnathorizedAccessException

From: Dmitry Shaporenkov (dsha1980_at_yahoo.com)
Date: 09/15/03


Date: Mon, 15 Sep 2003 01:09:53 -0700


>-----Original Message-----
>Hi all,
>
>I know this topis was already discussed in the
newsgroup,
>but I can't find any conclusions. The problem is that
>when I try to use RegistryKey.DeleteSubKeyTree, it
throws
>an UnauthorizedAccessException. I'am a local
>administrator on my machine, and the .NET framework
>configuration has not been changed since install time.
>The code snippet is as follows:
>.....
>RegistryKey CLSIDKey = Registry.ClassesRoot.OpenSubKey
>("CLSID");
>try
>{
> RegistryPermission regPerm = new RegistryPermission
>(RegistryPermissionAccess.Write, CLSIDKey.Name + "\\" +
>ourGUID);
> CLSIDKey.DeleteSubKeyTree (ourGUID);
>}
>catch (Exception e)
>{
> Console.WriteLine ("Can't delete CLSID " + ourGUID + "
>from registry. Details:" + e);
>}
>CLSIDKey.Close ();
>.....
>
>The output is:
>
>Can't delete CLSID <omitted> from registry.
>Details:System.UnauthorizedAccessException: Cannot write
>to the registry key.
> at Microsoft.Win32.RegistryKey.ValidateState(Boolean
>needWrite)
> at Microsoft.Win32.RegistryKey.DeleteSubKeyTree
(String
>subkey)
> at Registrar.Main(String[] args)
>
>Does anybody know what's the problem? How does the
>correct code look like?
>
  The problem was that I was opening the parent key
without write permissions. The correct code is:
...
>RegistryKey CLSIDKey = Registry.ClassesRoot.OpenSubKey
>("CLSID", true);
...

Ugh...



Relevant Pages

  • Re: Write to HKLM from windows Service
    ... I have what i believe to be the correct code but i am getting access violations. ... Cannot write to the registry key. ... My service is logged in a the local administrator and Permissions for that key for the local Admin are Full Control. ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: How do I grant permissions to registry key?
    ... To grant permissions to a registry key like you described programmatically, ... I think you should the RegistryPermission class ...
    (microsoft.public.vsnet.general)