BCUninstaller/Bulk-Crap-Uninstaller · error · ArgumentException

Path cannot point to a root key

Error message

Path cannot point to a root key

What it means

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).

Source

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

        /*/// <exception cref="ArgumentException">Path can't be empty or null</exception>
        public static string GetParentRegistryPath(string fullRegistryPath)
        {
            if (string.IsNullOrEmpty(fullRegistryPath))
                throw new ArgumentException("Path can't be empty or null", "fullRegistryPath");
            fullRegistryPath.TrimEnd('\\');
            var lastIndex = fullRegistryPath.LastIndexOf('\\');
            return fullRegistryPath.Substring(0, lastIndex);
        }*/

        public static void RemoveRegistryKey(string fullRegistryPath)
        {
            if (string.IsNullOrEmpty(fullRegistryPath))
                throw new ArgumentException(Localisation.RegistryTools_RemoveRegistryKey_PathEmptyNull,
                    nameof(fullRegistryPath));

            if (fullRegistryPath.Count(x => x.Equals('\\')) < 2)
                throw new ArgumentException(Localisation.RegistryTools_RemoveRegistryKey_PointsAtRoot,
                    nameof(fullRegistryPath));

            using (var key = OpenRegistryKey(Path.GetDirectoryName(fullRegistryPath), true))
            {
                if (key != null)
                {
                    var subkeyName = Path.GetFileName(fullRegistryPath);
                    // Check if key exists before attempting to remove to avoid an exception
                    if (key.GetSubKeyNames().Contains(subkeyName, StringComparison.OrdinalIgnoreCase))
                        key.DeleteSubKeyTree(subkeyName);
                }
            }
        }

        public static void RemoveRegistryValue(string fullRegistryPath, string valueName)
        {
            if (string.IsNullOrEmpty(fullRegistryPath))
                throw new ArgumentException(Localisation.RegistryTools_RemoveRegistryKey_PathEmptyNull,

View on GitHub (pinned to 608321de98)

Solutions

  1. Ensure the path has at least two backslashes, e.g. 'HKCU\\Software\\MyApp'.
  2. For deleting a direct child of a root, use Registry.LocalMachine.DeleteSubKey directly with explicit intent rather than this helper.
  3. Add a UI-side confirmation that refuses selections whose backslash count is under 2.

Example fix

// before
RegistryTools.RemoveRegistryKey("HKLM\\Software"); // only 1 backslash

// after
RegistryTools.RemoveRegistryKey("HKLM\\Software\\MyApp"); // 2 backslashes, safe
Defensive patterns

Strategy: validation

Validate before calling

if (fullRegistryPath.Count(c => c == '\\') < 2)
    throw new InvalidOperationException("Refusing to delete a root or first-level key: " + fullRegistryPath);

Type guard

static bool IsDeepEnoughToDelete(string s) =>
    !string.IsNullOrWhiteSpace(s) && s.Count(c => c == '\\') >= 2;

Try / catch

try { RegistryTools.RemoveRegistryKey(path); }
catch (ArgumentException ex) when (ex.Message.Contains("root"))
{ /* tell user this key is protected */ }

Prevention

When it happens

Trigger: Calling RemoveRegistryKey with 'HKLM', 'HKLM\\Software', or any path where Count('\\') < 2.

Common situations: 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.

Related errors


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