dotnet/wpf · error · ArgumentException

SR.DataObject_EmptyFormatNotAllowed

Error message

SR.DataObject_EmptyFormatNotAllowed

What it means

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.

Solutions

  1. Check string.IsNullOrEmpty(format) before calling GetDataFormat
  2. Provide a default well-known format (e.g. DataFormats.StringFormat) when input is empty
  3. Surface a validation error to the caller with the offending source of the empty string

Example fix

// before
var df = DataFormats.GetDataFormat(persistedFormatName);
// after
var df = string.IsNullOrEmpty(persistedFormatName)
    ? DataFormats.GetDataFormat(DataFormats.StringFormat)
    : DataFormats.GetDataFormat(persistedFormatName);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrEmpty(format)) throw new ArgumentException("Format must be non-empty", nameof(format));

Type guard

static bool IsValidClipboardFormat(string f) => !string.IsNullOrEmpty(f);

Try / catch

try
{
    var df = DataFormats.GetDataFormat(format);
}
catch (ArgumentException ex)
{
    // empty format name; use a default or report error
}

Prevention

When it happens

Trigger: 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.

Common situations: Clipboard interop code passing through a user-supplied or registry-supplied format string that is empty; deserializing format names from persisted settings.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/09d6f2da202add37. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/DataFormats.cs:29

public static class DataFormats
{
    private static bool s_initialized;

    /// <summary>
    ///  Gets an object with the Windows Clipboard numeric ID and name for the specified ID.
    /// </summary>
    public static DataFormat GetDataFormat(int id) => DataFormatsCore.GetOrAddFormat(id);

    /// <summary>
    ///  Gets the data format with the Windows Clipboard numeric ID and name for the specified data format.
    /// </summary>
    public static DataFormat GetDataFormat(string format)
    {
        ArgumentNullException.ThrowIfNull(format);

        if (format == string.Empty)
        {
            throw new ArgumentException(SR.DataObject_EmptyFormatNotAllowed);
        }

        // Ensures the predefined Win32 data formats into our format list.
        EnsurePredefined();

        return DataFormatsCore.GetOrAddFormat(format);
    }

    /// <summary>
    ///  Specifies the standard ANSI text format.
    /// </summary>
    public static readonly string Text = DataFormatNames.Text;

    /// <summary>
    ///  Specifies the standard Windows Unicode text format.
    /// </summary>
    public static readonly string UnicodeText = DataFormatNames.UnicodeText;

View on GitHub (pinned to 81131a70a4)