user4604440
user4604440

Reputation:

Delete a Registry key using C#

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

Upvotes: 2

Views: 13496

Answers (6)

Andrei Kalantarian
Andrei Kalantarian

Reputation: 355

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.

Upvotes: 0

Riccardo Bassilichi
Riccardo Bassilichi

Reputation: 969

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);

Upvotes: 2

DavidRR
DavidRR

Reputation: 19457

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.

Upvotes: 4

Rahul Tripathi
Rahul Tripathi

Reputation: 172608

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!

Upvotes: 1

jiten
jiten

Reputation: 5264

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");
    }

Upvotes: -1

Related Questions