{"record":{"id":"a45250c7072b3a97","repo":"d2phap/ImageGlass","slug":"cannot-open-registry-key-key","errorCode":null,"errorMessage":"Cannot open registry key: {key}","messagePattern":"Cannot open registry key: (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"v9/Components/ImageGlass.Base/WinApi/DesktopApi.cs","lineNumber":54,"sourceCode":"\n\npublic static partial class DesktopApi\n{\n    /// <summary>\n    /// Set the desktop wallpaper.\n    /// </summary>\n    /// <param name=\"bmpPath\">BMP image file path</param>\n    /// <param name=\"style\">Style of wallpaper</param>\n    /// <returns>Success/failure indication.</returns>\n    public static unsafe Exception? SetWallpaper(string bmpPath, WallpaperStyle style)\n    {\n        try\n        {\n            using var key = Registry.CurrentUser.OpenSubKey(@\"Control Panel\\Desktop\", true);\n\n            if (key == null)\n            {\n                throw new Exception($\"Cannot open registry key: {key}\");\n            }\n\n            var tileVal = \"0\"; // default not-tiled\n            var winStyle = \"1\"; // default centered\n\n            switch (style)\n            {\n                case WallpaperStyle.Tiled:\n                    tileVal = \"1\";\n                    break;\n\n                case WallpaperStyle.Stretched:\n                    winStyle = \"2\";\n                    break;\n\n                case WallpaperStyle.Current:\n                    tileVal = key.GetValue(\"TileWallpaper\")?.ToString() ?? \"\";\n                    winStyle = key.GetValue(\"WallpaperStyle\")?.ToString() ?? \"\";","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/d2phap/ImageGlass/blob/4a3c4feceffc5a8bb5e56ba836509634aaae47a9/v9/Components/ImageGlass.Base/WinApi/DesktopApi.cs#L36-L72","documentation":"Thrown inside the legacy v9 DesktopApi.SetWallpaper when Registry.CurrentUser.OpenSubKey(\"Control Panel\\Desktop\", writable:true) returns null (no write access, or key missing). Unlike the v10 Win32 path this method catches the exception and returns it as the result (signature Exception?), so callers receive the error object rather than seeing it propagate. The message carries the same interpolated-null defect as the v10 version: '{key}' is always null at the throw point.","triggerScenarios":"Calling DesktopApi.SetWallpaper on Windows from a context without write access to HKCU\\Control Panel\\Desktop (restricted user, kiosk, sandbox, group-policy lock, or a cleaned/locked registry). The returned Exception is then inspected/logged by the caller.","commonSituations":"MDM-locked enterprise machines; running as a standard user where the registry ACL denies writes; a registry security tool that revoked permissions; corrupted user hive.","solutions":["Inspect the returned Exception: if non-null, report 'cannot set wallpaper (registry access denied)' and run as a user with HKCU write access.","Verify the key with 'reg query \"HKCU\\Control Panel\\Desktop\"' and check the ACL grants the current user write.","Request a policy/ACL exception, or use an alternative wallpaper mechanism that does not require writing HKCU.","Fix the interpolated-null in the message so the returned error names the actual key path for diagnosis."],"exampleFix":"// before\nvar ex = DesktopApi.SetWallpaper(bmpPath, style);\nif (ex != null) throw ex;\n\n// after: meaningful handling\nvar ex = DesktopApi.SetWallpaper(bmpPath, style);\nif (ex != null)\n    Log.Warn($\"SetWallpaper failed (registry access): {ex.Message}\");","handlingStrategy":"validation","validationCode":"// Pre-check HKCU\\Control Panel\\Desktop writability (legacy v9 path).\nconst string KeyPath = @\"Control Panel\\Desktop\";\nusing var probe = Registry.CurrentUser.OpenSubKey(KeyPath, writable: true);\nif (probe is null) return new InvalidOperationException($\"No write access to HKCU\\\\{KeyPath}.\");","typeGuard":null,"tryCatchPattern":"var ex = DesktopApi.SetWallpaper(bmpPath, style);\nif (ex is not null) Log.Warn($\"Wallpaper not set: {ex.Message}\");","preventionTips":["Always inspect the returned Exception from the legacy SetWallpaper.","Ensure the user has write access to HKCU\\Control Panel\\Desktop.","Fix the interpolated-null '{key}' so the returned message is diagnostic."],"tags":["windows","registry","wallpaper","win32","permissions","legacy","imageglass","v9"],"backgroundTag":null,"analyzedSha":"4a3c4feceffc5a8bb5e56ba836509634aaae47a9","analyzedAt":"2026-08-13T16:58:15.523Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}