8

Trying to delete a subkey tree: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.hdr. .hdr subkey has one subkey, no values. So I use this code:

RegistryKey FileExts = Registry.CurrentUser.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts");
RegistryKey faulty = FileExts.OpenSubKey(".hdr");
Debug.Assert (faulty != null && faulty.SubKeyCount != 0);
faulty.Close();
FileExts.DeleteSubKeyTree(".hdr");

And I get the ArgumentException with message "Cannot delete a subkey tree because the subkey does not exist."

WTF? I checked and asserted it did exist?

Status update

Seeking with Process Monitor, the subkey of ".hdr" gets a ACCESS DENIED error when running the code. I checked the autorizations, but they look fine?

CharlesB
  • 86,532
  • 28
  • 194
  • 218
  • 2
    Chris may well be onto something: the registry is being virtualized, so you're looking at the wrong version. – Steven Sudit Jun 02 '10 at 16:24
  • OK, but how do I check it out? if I export the key in a .reg file, or if I print out the `faulty` string representation, it's just good old HKCU\blabla? – CharlesB Jun 02 '10 at 16:31
  • Well, start by reading this: http://msdn.microsoft.com/en-us/library/aa965884(VS.85).aspx – Steven Sudit Jun 02 '10 at 18:06
  • Steven: thanks for the link; it looks like I see an access denied error rather than a registry virtualization problem. I'll try run it with system privileges to check that. – CharlesB Jun 02 '10 at 18:10
  • any change in behavior if you remove the open/assert/close lines? Just curious if it still breaks if you never open .hdr or any of its children – James Manning Jun 03 '10 at 08:05
  • @James: no, it doesn't change anything. – CharlesB Jun 03 '10 at 09:28

3 Answers3

7

Found a solution, which raises other another question...

After pointing the ACCESS DENIED error with Process Monitor, I just tried to delete subkeys individually:

RegistryKey hdr = FileExts.OpenSubKey(".hdr", true);
foreach (String key in hdr.GetSubKeyNames())
   hdr.DeleteSubKey(key);
hdr.Close();
FileExts.DeleteSubKeyTree(".hdr");

It worked fine, so it's not a permission problem!

For a reason I don't understand, DeleteSubKeyTree needed an empty tree to work.

An explanation, anyone?

CharlesB
  • 86,532
  • 28
  • 194
  • 218
  • 2
    Thankyou, this was driving me crazy telling me the subkey didn't exist when it did. – midspace Jan 22 '16 at 02:05
  • 1
    I am able to recursively delete an extension key (e.g., `.hdr`) from the `FileExts` key via `DeleteSubKeyTree` using Visual Studio 2013 and .NET Framework 4.5. So, perhaps the problem that you encountered has been addressed. – DavidRR Jan 10 '17 at 18:30
1

It looks to me like you might be confusing the concept of a SubKey with a the concept of a Name/Value pair (it's at the very least not clear from the original post whether you actually mean a subkey or a Name/Value pair). It's easy to confuse the two so here's the skinny on the Registry...

Folders in the registry correspond to subkeys, while Name/Value pairs are where values are stored. When you hold a reference to a subkey (i.e. folder) and you want to delete a Name/Value pair inside that subkey, you call subKey.DeleteValue(name).

using(subkey = Registry.CurrentUser.OpenSubKey(subkeyName, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.FullControl))
{
    subKey.DeleteValue(nameValuePairName);
}
Alexander Høst
  • 918
  • 8
  • 16
0

I had the same issue but a different resolution. The DeleteSubKeyTree call raised ArgumentException saying the key didn't exist. It certainly does! I'm iterating over the existing key names and I am able to create a RegistryKey.

using (RegistryKey regClasses = Registry.ClassesRoot.OpenSubKey("CLSID", true))
{
    foreach (var class_guid in regClasses.GetSubKeyNames())
    {
        bool should_remove = false;
        using (RegistryKey productKey = regClasses.OpenSubKey(class_guid))
            should_remove = <some code here>
        if (should_remove)
            regClasses.DeleteSubKeyTree(class_guid);
    }
}

Then I realized I was debugging in user mode. When I ran as Admin it worked fine. Strange that the first OpenSubKey works in user mode, but when I open subkeys below this I correctly received Access Denied in user mode.

DaveSawyer
  • 351
  • 4
  • 8