BCUninstaller/Bulk-Crap-Uninstaller · error · IOException

Failed to set the last user registry key

Error message

Failed to set the last user registry key

What it means

Thrown by OpenRegKeyInRegedit when Registry.CurrentUser.CreateSubKey for the Regedit Applets key returns null. CreateSubKey returning null is unusual but possible if the OS denies creation or the hive is in a degraded state; the helper treats it as a hard failure because it cannot store the LastKey value regedit reads on startup.

Source

Thrown at source/KlocTools/Tools/RegistryTools.cs:451

            {
                File.Delete(tempFile);
            }
        }

        /// <exception cref="IOException">Could not complete this operation. </exception>
        public static void OpenRegKeyInRegedit(string registryPath)
        {
            try
            {
                if (!ProcessTools.SafeKillProcess("regedit"))
                    throw new IOException("Could not close running instance of Regedit");

                var regeditSettings =
                    Registry.CurrentUser.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Applets\Regedit",
                        RegistryKeyPermissionCheck.ReadWriteSubTree);

                if (regeditSettings == null)
                    throw new IOException("Failed to set the last user registry key");

                regeditSettings.SetValue("LastKey", registryPath, RegistryValueKind.String);
                Process.Start("regedit.exe");
            }
            catch (Exception ex)
            {
                throw new IOException("Failed to open regedit.exe", ex);
            }
        }
    }
}

View on GitHub (pinned to 608321de98)

Solutions

  1. Run in an interactive user session that has a writable HKCU hive loaded.
  2. Check group policy / registry ACLs on the Applets key and grant the user Create Subkey permission.
  3. Verify the user profile is not mandatory/corrupted; recreate it if NTUSER.DAT is suspect.

Example fix

// before
RegistryTools.OpenRegKeyInRegedit(path); // service account, no writable HKCU

// after (run in the user's interactive session, or set LastKey yourself)
using (var k = Registry.CurrentUser.CreateSubKey(
    @"Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Regedit",
    RegistryKeyPermissionCheck.ReadWriteSubTree))
{
    k?.SetValue("LastKey", path);
}
Process.Start("regedit.exe");
Defensive patterns

Strategy: try-catch

Validate before calling

using (var k = Registry.CurrentUser.OpenSubKey(
    @"Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Regedit", true))
{
    if (k == null) return; // Applets key not writable; do not call the helper
}

Try / catch

try { RegistryTools.OpenRegKeyInRegedit(path); }
catch (IOException ex) when (ex.Message.Contains("last user registry key"))
{ /* HKCU not writable; run in an interactive user session */ }

Prevention

When it happens

Trigger: Calling OpenRegKeyInRegedit when HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Regedit cannot be created or opened for ReadWriteSubTree, e.g. due to policy, quota, or a corrupted current-user hive.

Common situations: Group Policy restricting Applets key creation; roaming/mandatory profile with a locked HKCU; corrupted NTUSER.DAT; the current user has no writable profile loaded (service / session 0 contexts).

Related errors


AI-assisted analysis of BCUninstaller/Bulk-Crap-Uninstaller@608321de98 (2026-08-13). Data as JSON: /api/errors/6779fa07c2a10fa0. Report an issue: GitHub.