d2phap/ImageGlass · error · InvalidOperationException
IGE: Cannot open registry key: {key}
Error message
IGE: Cannot open registry key: {key} What it means
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.
Source
Thrown at source/ImageGlass.Win32/Common/WinAPI/Win32DesktopApi.cs:56
Span, // 22, 0 (for multi-monitor)
}
public static partial class Win32DesktopApi
{
/// <summary>
/// Sets the desktop wallpaper.
/// </summary>
/// <param name="filePath">Image file path</param>
/// <param name="style">Style of wallpaper</param>
/// <exception cref="Exception"></exception>
public static unsafe void SetWallpaper(string filePath, WallpaperStyle style)
{
// 1. open registry
using var key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", writable: true);
if (key is null)
{
throw new InvalidOperationException($"IGE: Cannot open registry key: {key}");
}
// 2. get the wallpaper style
(string bgStyle, string tileStyle) = style switch
{
WallpaperStyle.Fill => ("10", "0"),
WallpaperStyle.Fit => ("6", "0"),
WallpaperStyle.Stretch => ("2", "0"),
WallpaperStyle.Tile => ("0", "1"),
WallpaperStyle.Center => ("0", "0"),
WallpaperStyle.Span => ("22", "0"),
_ => ("-1", "0"),
};
// 3. check if we should use the current style
if (bgStyle == "-1")
{View on GitHub (pinned to 4a3c4fecef)
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.
Example fix
// before
using var key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", writable: true);
if (key is null) throw new InvalidOperationException($"IGE: Cannot open registry key: {key}");
// after: explicit path + clearer message, and a readable pre-check
const string KeyPath = @"Control Panel\Desktop";
using var key = Registry.CurrentUser.OpenSubKey(KeyPath, writable: true);
if (key is null)
throw new InvalidOperationException($"IGE: Cannot open registry key (access denied or missing): HKCU\\{KeyPath}"); Defensive patterns
Strategy: try-catch
Validate before calling
// Confirm HKCU\Control Panel\Desktop is writable before SetWallpaper.
const string KeyPath = @"Control Panel\Desktop";
using var probe = Registry.CurrentUser.OpenSubKey(KeyPath, writable: true);
if (probe is null)
throw new InvalidOperationException($"No write access to HKCU\\{KeyPath}."); Try / catch
try { Win32DesktopApi.SetWallpaper(filePath, style); }
catch (InvalidOperationException ex) when (ex.Message.Contains("Cannot open registry key"))
{ /* registry access denied — run as user with HKCU write access */ } Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Cannot open registry key: {key}
- IGE: This feature is not supported on macOS.
- The base64 content is invalid.
- IGE_001
- IGE_002
AI-assisted analysis of d2phap/ImageGlass@4a3c4fecef (2026-08-13).
Data as JSON: /api/errors/b9aded74f82dc8fc.
Report an issue: GitHub.