{"record":{"id":"7dc71fa6db30587c","repo":"BCUninstaller/Bulk-Crap-Uninstaller","slug":"could-not-close-running-instance-of-regedit","errorCode":null,"errorMessage":"Could not close running instance of Regedit","messagePattern":"Could not close running instance of Regedit","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"source/KlocTools/Tools/RegistryTools.cs","lineNumber":444,"sourceCode":"\n            try\n            {\n                RunRegeditCommand($\"/e \\\"{tempFile}\\\" \\\"{registryPath}\\\"\");\n                return File.ReadAllLines(tempFile, Encoding.Unicode);\n            }\n            finally\n            {\n                File.Delete(tempFile);\n            }\n        }\n\n        /// <exception cref=\"IOException\">Could not complete this operation. </exception>\n        public static void OpenRegKeyInRegedit(string registryPath)\n        {\n            try\n            {\n                if (!ProcessTools.SafeKillProcess(\"regedit\"))\n                    throw new IOException(\"Could not close running instance of Regedit\");\n\n                var regeditSettings =\n                    Registry.CurrentUser.CreateSubKey(@\"Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Regedit\",\n                        RegistryKeyPermissionCheck.ReadWriteSubTree);\n\n                if (regeditSettings == null)\n                    throw new IOException(\"Failed to set the last user registry key\");\n\n                regeditSettings.SetValue(\"LastKey\", registryPath, RegistryValueKind.String);\n                Process.Start(\"regedit.exe\");\n            }\n            catch (Exception ex)\n            {\n                throw new IOException(\"Failed to open regedit.exe\", ex);\n            }\n        }\n    }\n}","sourceCodeStart":426,"sourceCodeEnd":462,"githubUrl":"https://github.com/BCUninstaller/Bulk-Crap-Uninstaller/blob/608321de98e92297377b1eb69029af55c25504a1/source/KlocTools/Tools/RegistryTools.cs#L426-L462","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Run the calling process elevated so it can terminate an elevated regedit instance.","Prompt the user to close regedit manually before calling OpenRegKeyInRegedit.","Retry once after a short delay if SafeKillProcess is known to race in your environment."],"exampleFix":"// before\nRegistryTools.OpenRegKeyInRegedit(path); // fails if regedit already open elevated\n\n// after\nif (!ProcessTools.SafeKillProcess(\"regedit\"))\n    MessageBox.Show(\"Please close Registry Editor and try again.\");\nelse\n    RegistryTools.OpenRegKeyInRegedit(path);","handlingStrategy":"try-catch","validationCode":"if (!ProcessTools.SafeKillProcess(\"regedit\"))\n    return; // or prompt the user to close regedit manually","typeGuard":null,"tryCatchPattern":"try { RegistryTools.OpenRegKeyInRegedit(path); }\ncatch (IOException ex) when (ex.Message.Contains(\"close running instance\"))\n{ /* ask user to close Registry Editor, then retry */ }","preventionTips":["Run the calling process elevated so it can terminate an elevated regedit.","Prompt the user to close regedit before calling.","Avoid calling OpenRegKeyInRegedit from service/session-0 contexts."],"tags":["registry","regedit","process","permissions","windows","csharp"],"backgroundTag":null,"analyzedSha":"608321de98e92297377b1eb69029af55c25504a1","analyzedAt":"2026-08-13T12:17:35.389Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}