{"record":{"id":"bc5804c9c8b177ba","repo":"d2phap/ImageGlass","slug":"wallpaper-style-is-not-valid","errorCode":null,"errorMessage":"Wallpaper style is not valid.","messagePattern":"Wallpaper style is not valid\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"warning","filePath":"v9/igcmd/Functions.cs","lineNumber":54,"sourceCode":"    /// </summary>\n    /// <param name=\"imgPath\">Full path of image file</param>\n    /// <param name=\"styleStr\">Wallpaper style, see <see cref=\"WallpaperStyle\"/>.</param>\n    public static IgExitCode SetDesktopWallpaper(string imgPath, string styleStr)\n    {\n        return Run(() =>\n        {\n            if (Enum.TryParse(styleStr, out WallpaperStyle style))\n            {\n                var exception = DesktopApi.SetWallpaper(imgPath, style);\n\n                if (exception != null)\n                {\n                    throw exception;\n                }\n            }\n            else\n            {\n                throw new ArgumentException(\"Wallpaper style is not valid.\", styleStr);\n            }\n\n        }, (error) =>\n        {\n            _ = Config.ShowError(null,\n                description: error.Message,\n                title: Config.Language[\"FrmSettings._Theme._UninstallTheme\"]);\n        });\n    }\n\n\n    /// <summary>\n    /// Sets or unsets app extensions\n    /// </summary>\n    /// <param name=\"enable\"></param>\n    /// <param name=\"exts\">Extensions to proceed. Example: <c>.png;.jpg;</c></param>\n    public static IgExitCode SetAppExtensions(bool enable, string exts = \"\", bool perMachine = false)\n    {","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/d2phap/ImageGlass/blob/4a3c4feceffc5a8bb5e56ba836509634aaae47a9/v9/igcmd/Functions.cs#L36-L72","documentation":"Thrown in igcmd Functions.SetDesktopWallpaper when Enum.TryParse(styleStr, out WallpaperStyle) fails. WallpaperStyle is an int enum: Current=-1, Centered=0, Stretched=1, Tiled=2; Enum.TryParse is case-sensitive and parses by name (or a numeric string that fits the int), so any other token fails. It is an ArgumentException whose paramName is the offending styleStr itself. The throw happens inside Functions.Run, which catches it, sets IgExitCode.Error, and (if Program.ShowUi) shows the message via Config.ShowError.","triggerScenarios":"igcmd is invoked with the wallpaper command and the third command-line argument (styleStr) is not one of 'Current'/'Centered'/'Stretched'/'Tiled' (nor a numeric equivalent). Examples: lowercase 'tiled', a localized word, an out-of-range number, or a typo like 'Streched'.","commonSituations":"A user or script passes a localized style name; the calling UI sends the wrong casing; the arg is omitted and replaced by an unrelated token; or a config file stores the style under a different vocabulary than the enum names.","solutions":["Pass one of the exact enum names: 'Current', 'Centered', 'Stretched', or 'Tiled' (case-sensitive).","If accepting user input, normalize it to the enum name before calling SetDesktopWallpaper (e.g. map 'tile' -> 'Tiled').","Validate styleStr against the allowed set before invoking igcmd and surface a clear error at the caller.","Prefer passing the numeric value through the same channel only if it is in range [-1,2]."],"exampleFix":"// before\nreturn (int)Functions.SetDesktopWallpaper(CmdArgs[1], CmdArgs[2]);\n\n// after: whitelist + case-insensitive normalize before calling\nstatic readonly HashSet<string> ValidStyles =\n    new(StringComparer.OrdinalIgnoreCase) { \"Current\", \"Centered\", \"Stretched\", \"Tiled\" };\n\nvar rawStyle = CmdArgs[2];\nif (!ValidStyles.Contains(rawStyle))\n{\n    Console.Error.WriteLine($\"Invalid wallpaper style '{rawStyle}'. Use one of: {string.Join(\", \", ValidStyles)}.\");\n    return (int)IgExitCode.Error;\n}\n// Enum.TryParse below is case-sensitive, so pass the canonical-cased name\nvar canonical = ValidStyles.First(s => s.Equals(rawStyle, StringComparison.OrdinalIgnoreCase));\nreturn (int)Functions.SetDesktopWallpaper(CmdArgs[1], canonical);","handlingStrategy":"validation","validationCode":"// Whitelist and normalize the wallpaper style before calling SetDesktopWallpaper\nstatic readonly HashSet<string> ValidWallpaperStyles =\n    new(StringComparer.OrdinalIgnoreCase) { \"Current\", \"Centered\", \"Stretched\", \"Tiled\" };\n\nstatic bool TryNormalizeWallpaperStyle(string raw, out string canonical)\n{\n    canonical = ValidWallpaperStyles.FirstOrDefault(s => s.Equals(raw, StringComparison.OrdinalIgnoreCase));\n    return canonical != null;\n}","typeGuard":"static bool IsValidWallpaperStyle(string style) =>\n    style != null && ValidWallpaperStyles.Contains(style);  // case-insensitive set","tryCatchPattern":"// SetDesktopWallpaper already wraps the action in Functions.Run and shows Config.ShowError.\n// Treat a non-zero exit code (IgExitCode.Error) as the user-facing signal:\nvar code = Functions.SetDesktopWallpaper(imgPath, styleStr);\nif (code == IgExitCode.Error)\n{\n    logger.Warn(\"Invalid wallpaper style '{0}'.\", styleStr);\n}","preventionTips":["Always pass one of the exact enum names: Current, Centered, Stretched, Tiled.","Normalize user/script input case-insensitively against the enum names before invoking igcmd.","Validate the token at the calling UI/script before reaching igcmd to give a clearer error.","Do not store localized style words in config; store the enum name and localize only for display."],"tags":["wallpaper","validation","enum","desktop","igcmd","csharp"],"backgroundTag":null,"analyzedSha":"4a3c4feceffc5a8bb5e56ba836509634aaae47a9","analyzedAt":"2026-08-13T16:58:15.523Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}