2

I am trying to delete a Registry key like this:

RegistryKey oRegistryKey = Registry.CurrentUser.OpenSubKey(
    "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts", true);

oRegistryKey.DeleteSubKeyTree(".");

But that is giving me an exception:

Cannot delete a subkey tree because the subkey does not exist

If I change DeleteSubKeyTree to DeleteSubKey, I receive a different exception:

Registry key has subkeys and recursive removes are not supported by this method

DavidRR
  • 18,291
  • 25
  • 109
  • 191
  • 1
    Pretty logical errors I think, search them up in regedit.exe, verify the tree and adjust your code. – Xyv Aug 27 '15 at 13:08
  • I've verified this thing its there in the regedit at the same place. not getting what to do now –  Aug 27 '15 at 13:11
  • 2
    So you want to open Software\\Microsoft\\Windows\\CurrentVersion\\Explorer and DeleteSubKeyTree("FileExts"). Easy peasy. Careful with that chainsaw. – Hans Passant Aug 27 '15 at 13:15
  • The `o` prefix in your variable `oRegistryKey` represents what is known as [Systems Hungarian](http://stackoverflow.com/a/768316/1497596) notation. Microsoft discourages this variable naming practice when writing code for the .NET Framework. – DavidRR Jan 10 '17 at 19:21

6 Answers6

4

The approach outlined in this answer is needlessly complex because DeleteSubKeyTree is recursive. From its documentation on MSDN:

Deletes a subkey and any child subkeys recursively.

So, if your goal is to delete the user's FileExts key, do this:

string explorerKeyPath = @"Software\Microsoft\Windows\CurrentVersion\Explorer";

using (RegistryKey explorerKey =
    Registry.CurrentUser.OpenSubKey(explorerKeyPath, writable: true))
{
    if (explorerKey != null)
    {
        explorerKey.DeleteSubKeyTree("FileExts");
    }
}

However, are you sure that you really want to delete a user's FileExts key? I believe that most would consider doing so would be unreasonably destructive and reckless. A more common scenario would be deleting a single file extension key (e.g., .hdr) from the FileExts key.

Finally, note that DeleteSubKeyTree is overloaded. Here is the signature of the second version of this method:

public void DeleteSubKeyTree(
    string subkey,
    bool throwOnMissingSubKey
)

With this version, if subkey does not exist and throwOnMissingSubKey is false, DeleteSubKeyTree will simply return without making any changes to the Registry.

Community
  • 1
  • 1
DavidRR
  • 18,291
  • 25
  • 109
  • 191
1

Try this:

string str = @"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts";
string[] strSplit = strLocal.Split('\\');
            using (RegistryKey oRegistryKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts", true))
            {
                RegistryKey hdr = oRegistryKey.OpenSubKey(strSplit[strSplit.Length-2], true);
                foreach (String key in hdr.GetSubKeyNames())
                    hdr.DeleteSubKey(key);
                hdr.Close();
                oRegistryKey.DeleteSubKeyTree(strSplit[strSplit.Length - 2]);
            }

Also check: Registry in .NET: DeleteSubKeyTree says the subkey does not exists, but hey, it does!

Community
  • 1
  • 1
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

Please pay attention to 32 or 64 registry. NOTE: RegistryView.Registry64 it's the key of the problem.

This is my implementation that works for me:

public static void DeleteRegistryFolder(RegistryHive registryHive, string fullPathKeyToDelete)
{
    using (var baseKey = RegistryKey.OpenBaseKey(registryHive, RegistryView.Registry64))
    {
        baseKey.DeleteSubKeyTree(fullPathKeyToDelete);
    }
}

Usage:

var baseKeyString = $@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\MyApp";

DeleteRegistryFolder(RegistryHive.LocalMachine, baseKeyString);
0

This will help you ............

http://www.codeproject.com/Questions/166232/C-delete-a-registry-key-cannot-get-it-done

0

Both methods DeleteSubKey and DeleteSubKeyTree are deleting the current key if the 'subkey' parameter is empty string, like key.DeleteSubKey("");

Key should have write access. It has to be closed/disposed regular way.

-1

First you need to check the registry key:

using (RegistryKey Key = Registry.CurrentUser.OpenSubKey(@"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts"))
    if (Key != null)
    {    
       key.DeleteValue("."); //delete if exist
    }
    else
    {
        MessageBox.Show("key not found");
    }
jiten
  • 5,128
  • 4
  • 44
  • 73