'Deleting the icon associated with the file extension from the registry in C#

I use a method like this to associate the file extension with the icon:

public static void Associate(string extension, string progID, string description, string icon, string application)
{
    Registry.ClassesRoot.CreateSubKey(extension)?.SetValue("", progID);
    if (!string.IsNullOrEmpty(progID))
    {
        using var key = Registry.ClassesRoot.CreateSubKey(progID);
        if (description != null)
            key?.SetValue("", description);
        if (icon != null)
            key?.CreateSubKey("DefaultIcon")?.SetValue("", ToShortPathName(icon));
        if (application != null)
            key?.CreateSubKey(@"Shell\Open\Command")?.SetValue("", ToShortPathName(application) + " \"%1\"");
    }
}

When I want to completely delete the file extension that I have associated, I do not know how to proceed. For this, I tried something like this:

public static void Remove(string progID)
{
    Registry.ClassesRoot.DeleteSubKey(progID);
}

It gives an error like this:

The registry has subkeys and recursive removals are not supported by this method.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source