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
- 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
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
- 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
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
- InvalidEnumArgumentException(nameof(format), (int)format…
- SR.DataObject_EmptyFormatNotAllowed
- ' ' is not a valid value for ' '.
- ArgumentException(message, name)
- ArgumentNullException(name)
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)