c# - GetSubKeyNames getting exception "System.IO.IOException: No more data is available."

Viewed 350

I'm attempting to remove several registry keys in the event that a program doesn't uninstall correctly.

I'm compiling my program using Any CPU and running the program on Windows 10 Pro (64 bit).

I am receiving the following exception:

System.IO.IOException: No more data is available.
    at Microsoft.Win32.RegistryKey.Win32Error(Int32 errorCode, String str)
    at Microsoft.Win32.RegistryKey.InternalGetSubKeyNames()
    at Microsoft.Win32.RegistryKey.GetSubKeyNames()
    at ...

when I run the following code:

string[] items_list = new[]
{
    "string1",
    "string2",
    "string3"
    // ...
};

foreach (string subKeyName in Registry.ClassesRoot.GetSubKeyNames())
{
    if (items_list.Any(item => subKeyName.StartsWith(item)))
    {
        DeleteSubKeyTree(Registry.ClassesRoot, subKeyName);
    }
}

void DeleteSubKeyTree(RegistryKey regKey, string subKey)  
{
    try
    {
        regKey.DeleteSubKeyTree(subKey);
        WriteToLog($@"Deleting sub key {subKey} from {regKey.Name}.");
    }
    catch (Exception ex)
    {
        WriteToLog(ex.Message);
    }
}
0 Answers
Related