{"record":{"id":"09d6f2da202add37","repo":"dotnet/wpf","slug":"sr-dataobject-emptyformatnotallowed-dataformats","errorCode":null,"errorMessage":"SR.DataObject_EmptyFormatNotAllowed","messagePattern":"SR\\.DataObject_EmptyFormatNotAllowed","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/DataFormats.cs","lineNumber":29,"sourceCode":"public static class DataFormats\n{\n    private static bool s_initialized;\n\n    /// <summary>\n    ///  Gets an object with the Windows Clipboard numeric ID and name for the specified ID.\n    /// </summary>\n    public static DataFormat GetDataFormat(int id) => DataFormatsCore.GetOrAddFormat(id);\n\n    /// <summary>\n    ///  Gets the data format with the Windows Clipboard numeric ID and name for the specified data format.\n    /// </summary>\n    public static DataFormat GetDataFormat(string format)\n    {\n        ArgumentNullException.ThrowIfNull(format);\n\n        if (format == string.Empty)\n        {\n            throw new ArgumentException(SR.DataObject_EmptyFormatNotAllowed);\n        }\n\n        // Ensures the predefined Win32 data formats into our format list.\n        EnsurePredefined();\n\n        return DataFormatsCore.GetOrAddFormat(format);\n    }\n\n    /// <summary>\n    ///  Specifies the standard ANSI text format.\n    /// </summary>\n    public static readonly string Text = DataFormatNames.Text;\n\n    /// <summary>\n    ///  Specifies the standard Windows Unicode text format.\n    /// </summary>\n    public static readonly string UnicodeText = DataFormatNames.UnicodeText;\n","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/DataFormats.cs#L11-L47","documentation":"DataFormats.GetDataFormat(string format) looks up or registers a clipboard data format by name and rejects null and the empty string with ArgumentException (SR.DataObject_EmptyFormatNotAllowed), because an empty Win32 format name cannot be registered.","triggerScenarios":"Calling DataFormats.GetDataFormat(\"\") — e.g. with a format name from an empty config value, an uninitialized field, or a split/parse result that yielded an empty token.","commonSituations":"Clipboard interop code passing through a user-supplied or registry-supplied format string that is empty; deserializing format names from persisted settings.","solutions":["Check string.IsNullOrEmpty(format) before calling GetDataFormat","Provide a default well-known format (e.g. DataFormats.StringFormat) when input is empty","Surface a validation error to the caller with the offending source of the empty string"],"exampleFix":"// before\nvar df = DataFormats.GetDataFormat(persistedFormatName);\n// after\nvar df = string.IsNullOrEmpty(persistedFormatName)\n    ? DataFormats.GetDataFormat(DataFormats.StringFormat)\n    : DataFormats.GetDataFormat(persistedFormatName);","handlingStrategy":"validation","validationCode":"if (string.IsNullOrEmpty(format)) throw new ArgumentException(\"Format must be non-empty\", nameof(format));","typeGuard":"static bool IsValidClipboardFormat(string f) => !string.IsNullOrEmpty(f);","tryCatchPattern":"try\n{\n    var df = DataFormats.GetDataFormat(format);\n}\ncatch (ArgumentException ex)\n{\n    // empty format name; use a default or report error\n}","preventionTips":["Guard against empty values from config/registry before lookup","Default to a well-known format when input is empty","Validate persisted format names when loading settings"],"tags":["wpf","clipboard","argument-validation"],"backgroundTag":"empty-required-field","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}