Registry in .NET: DeleteSubKeyTree says the section does not exist, but hey, it does!

Attempting to delete a subsection tree: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.hdr . .hdr subsection has one subsection, no values. Therefore, 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 an ArgumentException message with the message "Cannot delete the subsection tree because the child section does not exist."

WTF? I checked and claimed that it exists?

Status Update

Searching with Process Monitor, subsection ".hdr" receives an ACCESS DENIED error when running the code. I checked the authorizations, but they look fine?

+5
source share
3 answers

Found a solution that raises another question ...

After pointing out the ACCESS DENIED in Process Monitor, I just tried to delete individual subkeys:

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

It worked fine, so this is not a resolution problem!

For some reason, I don’t understand, for DeleteSubKeyTree it was necessary to work with an empty tree.

Explanation, anyone?

+6
source

I had the same problem, but a different resolution. Calling DeleteSubKeyTree raised an ArgumentException, saying that the key does not exist. Of course! I repeat the existing key names and I can create 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 that I was debugging in user mode. When I worked as Admin, it worked fine. It is strange that the first OpenSubKey works in user mode, but when I open the sections below this, I got Access Denied in user mode correctly.

0
source

It seems to me that you are possibly confusing the concept of SubKey with the concept of a name / value pair (at least it is not clear from the original message whether you mean a subsection or a name / value pair). It's easy to mix up, so here is skinny in the registry ...

The folders in the registry correspond to the subkeys, and the Name / Value pairs are where the values ​​are stored. When you hold a link to a subsection (i.e. a folder) and want to delete the Name / Value pair inside this subsection, you call subKey.DeleteValue(name) .

 using(subkey = Registry.CurrentUser.OpenSubKey(subkeyName, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.FullControl)) { subKey.DeleteValue(nameValuePairName); } 
0
source

Source: https://habr.com/ru/post/1311634/


All Articles