BCUninstaller/Bulk-Crap-Uninstaller · error · IOException

Could not close running instance of Regedit

Error message

Could not close running instance of Regedit

What it means

Thrown by OpenRegKeyInRegedit when ProcessTools.SafeKillProcess("regedit") returns false, meaning a running regedit instance could not be terminated before launching a fresh one. The helper insists on a clean regedit start so it can set the LastKey value and have regedit open on the target key.

Source

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

            try
            {
                RunRegeditCommand($"/e \"{tempFile}\" \"{registryPath}\"");
                return File.ReadAllLines(tempFile, Encoding.Unicode);
            }
            finally
            {
                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 the calling process elevated so it can terminate an elevated regedit instance.
  2. Prompt the user to close regedit manually before calling OpenRegKeyInRegedit.
  3. Retry once after a short delay if SafeKillProcess is known to race in your environment.

Example fix

// before
RegistryTools.OpenRegKeyInRegedit(path); // fails if regedit already open elevated

// after
if (!ProcessTools.SafeKillProcess("regedit"))
    MessageBox.Show("Please close Registry Editor and try again.");
else
    RegistryTools.OpenRegKeyInRegedit(path);
Defensive patterns

Strategy: try-catch

Validate before calling

if (!ProcessTools.SafeKillProcess("regedit"))
    return; // or prompt the user to close regedit manually

Try / catch

try { RegistryTools.OpenRegKeyInRegedit(path); }
catch (IOException ex) when (ex.Message.Contains("close running instance"))
{ /* ask user to close Registry Editor, then retry */ }

Prevention

When it happens

Trigger: Calling OpenRegKeyInRegedit while regedit is already running and the current process lacks the rights to kill it, or the kill races and the process is still alive when checked.

Common situations: Non-admin process trying to kill an elevated regedit; user has regedit open interactively; antivirus protecting regedit.exe from termination; terminal-session isolation preventing cross-session kills.

Related errors


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