{"record":{"id":"b9aded74f82dc8fc","repo":"d2phap/ImageGlass","slug":"ige-cannot-open-registry-key-key","errorCode":null,"errorMessage":"IGE: Cannot open registry key: {key}","messagePattern":"IGE: Cannot open registry key: (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"source/ImageGlass.Win32/Common/WinAPI/Win32DesktopApi.cs","lineNumber":56,"sourceCode":"    Span,       // 22, 0 (for multi-monitor)\n}\n\n\npublic static partial class Win32DesktopApi\n{\n    /// <summary>\n    /// Sets the desktop wallpaper.\n    /// </summary>\n    /// <param name=\"filePath\">Image file path</param>\n    /// <param name=\"style\">Style of wallpaper</param>\n    /// <exception cref=\"Exception\"></exception>\n    public static unsafe void SetWallpaper(string filePath, WallpaperStyle style)\n    {\n        // 1. open registry\n        using var key = Registry.CurrentUser.OpenSubKey(@\"Control Panel\\Desktop\", writable: true);\n        if (key is null)\n        {\n            throw new InvalidOperationException($\"IGE: Cannot open registry key: {key}\");\n        }\n\n\n        // 2. get the wallpaper style\n        (string bgStyle, string tileStyle) = style switch\n        {\n            WallpaperStyle.Fill => (\"10\", \"0\"),\n            WallpaperStyle.Fit => (\"6\", \"0\"),\n            WallpaperStyle.Stretch => (\"2\", \"0\"),\n            WallpaperStyle.Tile => (\"0\", \"1\"),\n            WallpaperStyle.Center => (\"0\", \"0\"),\n            WallpaperStyle.Span => (\"22\", \"0\"),\n            _ => (\"-1\", \"0\"),\n        };\n\n        // 3. check if we should use the current style\n        if (bgStyle == \"-1\")\n        {","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/d2phap/ImageGlass/blob/4a3c4feceffc5a8bb5e56ba836509634aaae47a9/source/ImageGlass.Win32/Common/WinAPI/Win32DesktopApi.cs#L38-L74","documentation":"Thrown by Win32DesktopApi.SetWallpaper when Registry.CurrentUser.OpenSubKey(\"Control Panel\\Desktop\", writable:true) returns null. Opening with writable:true requires write permission; if the current user lacks write access to that hive, or the key has been deleted/redirected, OpenSubKey returns null and the method throws InvalidOperationException. Note a logging defect: the message interpolates '{key}' after key is already null, so it always renders empty — the real cause must be diagnosed from permissions/policy. This is the v10 Win32 wallpaper path.","triggerScenarios":"Invoking SetWallpaper on Windows under an account that cannot write to HKCU\\Control Panel\\Desktop (locked-down kiosk, restricted group policy, sandbox, or a registry-virtualization redirect that denies write). Also if a registry cleaner/security tool removed or locked the key.","commonSituations":"Enterprise/MDM-managed machines where HKCU writes are restricted; running from a low-privilege or app-container context; corrupted user registry hive; the key existing but ACL-denied for the current user.","solutions":["Run ImageGlass (or the wallpaper action) as the interactive user with write access to HKCU; verify the account is not subject to a registry-write group policy.","Check the key exists and is writable before calling: open it in Registry Editor and confirm permissions, or run 'reg query \"HKCU\\Control Panel\\Desktop\" /v WallpaperStyle' to confirm read/write.","If running in a locked-down environment, request an exception / adjust the ACL on HKCU\\Control Panel\\Desktop for the user, or fall back to a non-registry wallpaper mechanism.","Wrap the call in try/catch (InvalidOperationException) and show a clear message; consider fixing the interpolated-null defect so the error names the actual key path."],"exampleFix":"// before\nusing var key = Registry.CurrentUser.OpenSubKey(@\"Control Panel\\Desktop\", writable: true);\nif (key is null) throw new InvalidOperationException($\"IGE: Cannot open registry key: {key}\");\n\n// after: explicit path + clearer message, and a readable pre-check\nconst string KeyPath = @\"Control Panel\\Desktop\";\nusing var key = Registry.CurrentUser.OpenSubKey(KeyPath, writable: true);\nif (key is null)\n    throw new InvalidOperationException($\"IGE: Cannot open registry key (access denied or missing): HKCU\\\\{KeyPath}\");","handlingStrategy":"try-catch","validationCode":"// Confirm HKCU\\Control Panel\\Desktop is writable before SetWallpaper.\nconst string KeyPath = @\"Control Panel\\Desktop\";\nusing var probe = Registry.CurrentUser.OpenSubKey(KeyPath, writable: true);\nif (probe is null)\n    throw new InvalidOperationException($\"No write access to HKCU\\\\{KeyPath}.\");","typeGuard":null,"tryCatchPattern":"try { Win32DesktopApi.SetWallpaper(filePath, style); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Cannot open registry key\"))\n{ /* registry access denied — run as user with HKCU write access */ }","preventionTips":["Ensure the interactive user has write access to HKCU\\Control Panel\\Desktop.","On managed/kiosk machines, request a policy exception for that key before relying on SetWallpaper.","Fix the interpolated-null '{key}' in the message so the error names the actual key path."],"tags":["windows","registry","wallpaper","win32","permissions","invalidoperationexception","imageglass"],"backgroundTag":null,"analyzedSha":"4a3c4feceffc5a8bb5e56ba836509634aaae47a9","analyzedAt":"2026-08-13T16:58:15.523Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}