{"record":{"id":"6779fa07c2a10fa0","repo":"BCUninstaller/Bulk-Crap-Uninstaller","slug":"failed-to-set-the-last-user-registry-key","errorCode":null,"errorMessage":"Failed to set the last user registry key","messagePattern":"Failed to set the last user registry key","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"source/KlocTools/Tools/RegistryTools.cs","lineNumber":451,"sourceCode":"            {\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":433,"sourceCodeEnd":462,"githubUrl":"https://github.com/BCUninstaller/Bulk-Crap-Uninstaller/blob/608321de98e92297377b1eb69029af55c25504a1/source/KlocTools/Tools/RegistryTools.cs#L433-L462","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","solutions":["Run in an interactive user session that has a writable HKCU hive loaded.","Check group policy / registry ACLs on the Applets key and grant the user Create Subkey permission.","Verify the user profile is not mandatory/corrupted; recreate it if NTUSER.DAT is suspect."],"exampleFix":"// before\nRegistryTools.OpenRegKeyInRegedit(path); // service account, no writable HKCU\n\n// after (run in the user's interactive session, or set LastKey yourself)\nusing (var k = Registry.CurrentUser.CreateSubKey(\n    @\"Software\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Applets\\\\Regedit\",\n    RegistryKeyPermissionCheck.ReadWriteSubTree))\n{\n    k?.SetValue(\"LastKey\", path);\n}\nProcess.Start(\"regedit.exe\");","handlingStrategy":"try-catch","validationCode":"using (var k = Registry.CurrentUser.OpenSubKey(\n    @\"Software\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Applets\\\\Regedit\", true))\n{\n    if (k == null) return; // Applets key not writable; do not call the helper\n}","typeGuard":null,"tryCatchPattern":"try { RegistryTools.OpenRegKeyInRegedit(path); }\ncatch (IOException ex) when (ex.Message.Contains(\"last user registry key\"))\n{ /* HKCU not writable; run in an interactive user session */ }","preventionTips":["Run in an interactive session with a writable HKCU.","Check group policy restricting the Applets key.","Validate the user profile (NTUSER.DAT) is healthy before use."],"tags":["registry","regedit","permissions","current-user","windows","csharp"],"backgroundTag":null,"analyzedSha":"608321de98e92297377b1eb69029af55c25504a1","analyzedAt":"2026-08-13T12:17:35.389Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}