RegistryKey.DeleteSubKeyTree throws an UnathorizedAccessException
From: Dmitry Shaporenkov (dsha1980_at_yahoo.com)
Date: 09/15/03
- Next message: David Cross [MS]: "Re: PKCS #7"
- Previous message: Govind: "PKCS #7"
- In reply to: Dmitry Shaporenkov: "RegistryKey.DeleteSubKeyTree throws an UnathorizedAccessException"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
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...
- Next message: David Cross [MS]: "Re: PKCS #7"
- Previous message: Govind: "PKCS #7"
- In reply to: Dmitry Shaporenkov: "RegistryKey.DeleteSubKeyTree throws an UnathorizedAccessException"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Relevant Pages
|