SetData Method

Clipboard.SetData Method (String, Object)

 

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

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public static void SetData(
	string format,
	object data
)

Parameters

format
Type: System.String

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

data
Type: System.Object

An Object representing the data to add.

Exception Condition
ExternalException

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

ThreadStateException

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

ArgumentNullException

data is null.

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.

System_CAPS_noteNote

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.

The following example demonstrates this member.

// 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;
}

UIPermission

for adding data to the system Clipboard that is of type String or is in one of the following data formats: Text, UnicodeText, or CommaSeparatedValue. Associated enumeration: UIPermissionClipboard.OwnClipboard

UIPermission

for adding data of any type or format to the system Clipboard. Associated enumeration: UIPermissionClipboard.AllClipboard

.NET Framework
Available since 2.0
Return to top
Show:
© 2016 Microsoft