{"record":{"id":"e74710a6f06d11b5","repo":"BCUninstaller/Bulk-Crap-Uninstaller","slug":"path-cannot-point-to-a-root-key","errorCode":null,"errorMessage":"Path cannot point to a root key","messagePattern":"Path cannot point to a root key","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"source/KlocTools/Tools/RegistryTools.cs","lineNumber":352,"sourceCode":"\n        /*/// <exception cref=\"ArgumentException\">Path can't be empty or null</exception>\n        public static string GetParentRegistryPath(string fullRegistryPath)\n        {\n            if (string.IsNullOrEmpty(fullRegistryPath))\n                throw new ArgumentException(\"Path can't be empty or null\", \"fullRegistryPath\");\n            fullRegistryPath.TrimEnd('\\\\');\n            var lastIndex = fullRegistryPath.LastIndexOf('\\\\');\n            return fullRegistryPath.Substring(0, lastIndex);\n        }*/\n\n        public static void RemoveRegistryKey(string fullRegistryPath)\n        {\n            if (string.IsNullOrEmpty(fullRegistryPath))\n                throw new ArgumentException(Localisation.RegistryTools_RemoveRegistryKey_PathEmptyNull,\n                    nameof(fullRegistryPath));\n\n            if (fullRegistryPath.Count(x => x.Equals('\\\\')) < 2)\n                throw new ArgumentException(Localisation.RegistryTools_RemoveRegistryKey_PointsAtRoot,\n                    nameof(fullRegistryPath));\n\n            using (var key = OpenRegistryKey(Path.GetDirectoryName(fullRegistryPath), true))\n            {\n                if (key != null)\n                {\n                    var subkeyName = Path.GetFileName(fullRegistryPath);\n                    // Check if key exists before attempting to remove to avoid an exception\n                    if (key.GetSubKeyNames().Contains(subkeyName, StringComparison.OrdinalIgnoreCase))\n                        key.DeleteSubKeyTree(subkeyName);\n                }\n            }\n        }\n\n        public static void RemoveRegistryValue(string fullRegistryPath, string valueName)\n        {\n            if (string.IsNullOrEmpty(fullRegistryPath))\n                throw new ArgumentException(Localisation.RegistryTools_RemoveRegistryKey_PathEmptyNull,","sourceCodeStart":334,"sourceCodeEnd":370,"githubUrl":"https://github.com/BCUninstaller/Bulk-Crap-Uninstaller/blob/608321de98e92297377b1eb69029af55c25504a1/source/KlocTools/Tools/RegistryTools.cs#L334-L370","documentation":"Thrown by RemoveRegistryKey when fullRegistryPath contains fewer than 2 backslashes (localized key RegistryTools_RemoveRegistryKey_PointsAtRoot). The library forbids deleting a registry root or a direct child of the root; you must specify a key at least two levels deep (e.g. HKLM\\\\Software\\\\App has 2 backslashes).","triggerScenarios":"Calling RemoveRegistryKey with 'HKLM', 'HKLM\\\\Software', or any path where Count('\\\\') < 2.","commonSituations":"User selecting a top-level node in a registry browser; auto-generated cleanup targeting a hive-wide key; off-by-one in path trimming that drops the leaf segment.","solutions":["Ensure the path has at least two backslashes, e.g. 'HKCU\\\\Software\\\\MyApp'.","For deleting a direct child of a root, use Registry.LocalMachine.DeleteSubKey directly with explicit intent rather than this helper.","Add a UI-side confirmation that refuses selections whose backslash count is under 2."],"exampleFix":"// before\nRegistryTools.RemoveRegistryKey(\"HKLM\\\\Software\"); // only 1 backslash\n\n// after\nRegistryTools.RemoveRegistryKey(\"HKLM\\\\Software\\\\MyApp\"); // 2 backslashes, safe","handlingStrategy":"validation","validationCode":"if (fullRegistryPath.Count(c => c == '\\\\') < 2)\n    throw new InvalidOperationException(\"Refusing to delete a root or first-level key: \" + fullRegistryPath);","typeGuard":"static bool IsDeepEnoughToDelete(string s) =>\n    !string.IsNullOrWhiteSpace(s) && s.Count(c => c == '\\\\') >= 2;","tryCatchPattern":"try { RegistryTools.RemoveRegistryKey(path); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"root\"))\n{ /* tell user this key is protected */ }","preventionTips":["Disable the delete action in the UI for top-level nodes.","Recompute the backslash count after any path trimming.","Treat the guard as a feature: it prevents wiping hive-level keys."],"tags":["registry","argument-validation","safety-guard","windows","csharp"],"backgroundTag":null,"analyzedSha":"608321de98e92297377b1eb69029af55c25504a1","analyzedAt":"2026-08-13T12:17:35.389Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}