Clipboard.SetData(String, Object) Method

Definition

Clears the Clipboard and then adds data in the specified format.

C#
public static void SetData(string format, object data);

Parameters

format
String

The format of the data to set. See DataFormats for predefined formats.

data
Object

An Object representing the data to add.

Exceptions

The Clipboard could not be cleared. This typically occurs when the Clipboard is being used by another process.

The current thread is not in single-threaded apartment (STA) mode. Add the STAThreadAttribute to your application's Main method.

data is null.

Examples

The following example demonstrates this member.

C#
// Demonstrates SetData, ContainsData, and GetData.
public Object SwapClipboardFormattedData(String format, Object data)
{
    Object returnObject = null;
    if (Clipboard.ContainsData(format))
    {
        returnObject = Clipboard.GetData(format);
        Clipboard.SetData(format, data);
    }
    return returnObject;
}

Remarks

If you do not know the format of the target application, you can store data in multiple formats using this method.

Data stored using this method can be converted to a compatible format when it is retrieved.

To retrieve data from the Clipboard in a particular format, first use the ContainsData method to determine whether the Clipboard contains data in that format before retrieving it with the GetData method.

Note

The Clipboard class can only be used in threads set to single thread apartment (STA) mode. To use this class, ensure that your Main method is marked with the STAThreadAttribute attribute.

Applies to

Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

See also