Edit

Share via


How to add data to the Clipboard

The Clipboard class provides methods that interact with the Windows operating system Clipboard feature. Many applications use the Clipboard as a temporary repository for data. For example, word processors use the Clipboard during cut-and-paste operations. The Clipboard also transfers data from one application to another.

When you add data to the Clipboard, indicate the data format so that other applications can recognize the data if they can use that format. Add data to the Clipboard in multiple different formats to increase the number of other applications that can potentially use the data.

A Clipboard format is a string that identifies the format so that an application using that format can retrieve the associated data. The DataFormats class provides predefined format names for your use. You can also use your own format names or use an object's type as its format.

Note

All Windows-based applications share the Clipboard. Therefore, the contents are subject to change when you switch to another application.

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.

To add data to the Clipboard in one or multiple formats, use the SetDataObject method. Pass any object to this method. To add data in multiple formats, first add the data to a separate object designed to work with multiple formats. Typically, add your data to a DataObject, but you can use any type that implements the IDataObject interface.

To add data to the Clipboard in a single, common format, use the specific method for that format, such as SetText for text.

Important

Custom objects must be serializable to JSON for them to be put on the Clipboard. Use the new type-safe methods like SetDataAsJson which automatically handle JSON serialization. The legacy SetData() method no longer works with custom objects starting with .NET 9 due to the removal of BinaryFormatter.

Add data in a single format

Use the SetAudio, SetFileDropList, SetImage, or SetText method.

// Demonstrates SetAudio, ContainsAudio, and GetAudioStream.
public Stream SwapClipboardAudio(Stream replacementAudioStream)
{
    Stream? returnAudioStream = null;

    if (Clipboard.ContainsAudio())
    {
        returnAudioStream = Clipboard.GetAudioStream();
        Clipboard.SetAudio(replacementAudioStream);
    }
    return returnAudioStream;
}

// Demonstrates SetFileDropList, ContainsFileDroList, and GetFileDropList
public StringCollection SwapClipboardFileDropList(StringCollection replacementList)
{
    StringCollection? returnList = null;

    if (Clipboard.ContainsFileDropList())
    {
        returnList = Clipboard.GetFileDropList();
        Clipboard.SetFileDropList(replacementList);
    }
    return returnList;
}

// Demonstrates SetImage, ContainsImage, and GetImage.
public Image SwapClipboardImage(Image replacementImage)
{
    Image? returnImage = null;

    if (Clipboard.ContainsImage())
    {
        returnImage = Clipboard.GetImage();
        Clipboard.SetImage(replacementImage);
    }
    return returnImage;
}

// Demonstrates SetText, ContainsText, and GetText.
public string SwapClipboardHtmlText(string replacementHtmlText)
{
    string? returnHtmlText = null;

    if (Clipboard.ContainsText(TextDataFormat.Html))
    {
        returnHtmlText = Clipboard.GetText(TextDataFormat.Html);
        Clipboard.SetText(replacementHtmlText, TextDataFormat.Html);
    }
    return returnHtmlText;
}
' Demonstrates SetAudio, ContainsAudio, and GetAudioStream.
Public Function SwapClipboardAudio(
    ByVal replacementAudioStream As Stream) As Stream

    Dim returnAudioStream As Stream = Nothing

    If Clipboard.ContainsAudio() Then
        returnAudioStream = Clipboard.GetAudioStream()
        Clipboard.SetAudio(replacementAudioStream)
    End If

    Return returnAudioStream

End Function

' Demonstrates SetFileDropList, ContainsFileDroList, and GetFileDropList
Public Function SwapClipboardFileDropList(ByVal replacementList As StringCollection) As StringCollection

    Dim returnList As StringCollection = Nothing

    If Clipboard.ContainsFileDropList() Then
        returnList = Clipboard.GetFileDropList()
        Clipboard.SetFileDropList(replacementList)
    End If

    Return returnList

End Function

' Demonstrates SetImage, ContainsImage, and GetImage.
Public Function SwapClipboardImage(
    ByVal replacementImage As Image) As Image

    Dim returnImage As Image = Nothing

    If Clipboard.ContainsImage() Then
        returnImage = Clipboard.GetImage()
        Clipboard.SetImage(replacementImage)
    End If

    Return returnImage
End Function

' Demonstrates SetText, ContainsText, and GetText.
Public Function SwapClipboardHtmlText(
    ByVal replacementHtmlText As String) As String

    Dim returnHtmlText As String = Nothing

    If Clipboard.ContainsText(TextDataFormat.Html) Then
        returnHtmlText = Clipboard.GetText(TextDataFormat.Html)
        Clipboard.SetText(replacementHtmlText, TextDataFormat.Html)
    End If

    Return returnHtmlText

End Function

Add data in a custom format

Use the Clipboard.SetDataAsJson<T>(String, T) method with a custom format name and your object.

The SetDataAsJson<T>() method automatically serializes your custom objects using System.Text.Json. This is the recommended approach in .NET 10 and later for storing custom types on the clipboard, as it provides type safety and security advantages over the legacy SetData() method.

Important

The legacy SetData method no longer works with custom objects in .NET 9 and later due to the removal of BinaryFormatter. Use SetDataAsJson<T>() instead for custom types.

// Demonstrates SetDataAsJson, ContainsData, and GetData
// using a custom format name and a business object.
public Customer TestCustomFormat
{
    get
    {
        Clipboard.SetDataAsJson("CustomerFormat", new Customer("Customer Name"));
        if (Clipboard.ContainsData("CustomerFormat"))
        {
            if (Clipboard.TryGetData("CustomerFormat", out Customer customerData))
                return customerData;
        }

        return null;
    }
}
' Demonstrates SetData, ContainsData, and GetData
' using a custom format name and a business object.
Public ReadOnly Property TestCustomFormat() As Customer
    Get
        Clipboard.SetDataAsJson("CustomerFormat", New Customer("Customer Name"))

        If Clipboard.ContainsData("CustomerFormat") Then
            Dim customerData As Customer = Nothing

            If Clipboard.TryGetData("CustomerFormat", customerData) Then
                Return customerData
            End If

        End If

        Return Nothing
    End Get
End Property

The Customer class used in the previous snippet:

// Customer class used in custom clipboard format examples.
public class Customer
{
    public string Name { get; set; }
    
    public Customer(string name)
    {
        Name = name;
    }
}
'Customer class used in custom clipboard format examples.
Public Class Customer

    Public Property Name As String

    Public Sub New(ByVal name As String)
        Me.Name = name
    End Sub

End Class

Add data in multiple formats

Use the Clipboard.SetDataObject method and pass in a DataObject that contains your data.

// Demonstrates how to use a DataObject to add
// data to the Clipboard in multiple formats.
public void TestClipboardMultipleFormats()
{
    DataObject data = new();

    Customer customer = new("Customer #2112");
    ListViewItem listViewItem = new($"Customer as ListViewItem {customer.Name}");

    // Add a Customer object using the type as the format.
    data.SetDataAsJson(customer);

    // Add a ListViewItem object using a custom format name.
    data.SetDataAsJson("ListViewItemFormat", listViewItem.Text);

    Clipboard.SetDataObject(data);

    // Retrieve the data from the Clipboard.
    DataObject retrievedData = (DataObject)Clipboard.GetDataObject()!;

    if (retrievedData.GetDataPresent("ListViewItemFormat"))
    {
        if (retrievedData.TryGetData("ListViewItemFormat", out String item))
        {
            ListViewItem recreatedListViewItem = new(item);
            MessageBox.Show($"Data contains ListViewItem with text of {recreatedListViewItem.Text}");
        }
    }

    if (retrievedData.GetDataPresent(typeof(Customer)))
    {
        if (retrievedData.TryGetData(out Customer newCustomer))
        {
            MessageBox.Show($"Data contains Customer with name of {newCustomer.Name}");
        }
    }
}
' Demonstrates how to use a DataObject to add
' data to the Clipboard in multiple formats.
Public Sub TestClipboardMultipleFormats()

    Dim data As New DataObject()

    Dim customer As New Customer("Customer #2112")
    Dim listViewItem As New ListViewItem($"Customer as ListViewItem {customer.Name}")

    ' Add a Customer object using the type as the format.
    data.SetDataAsJson(customer)

    ' Add a ListViewItem object using a custom format name.
    data.SetDataAsJson("ListViewItemFormat", listViewItem.Text)

    Clipboard.SetDataObject(data)

    ' Retrieve the data from the Clipboard.
    Dim retrievedData As DataObject = CType(Clipboard.GetDataObject(), DataObject)

    If retrievedData.GetDataPresent("ListViewItemFormat") Then
        Dim item As String = Nothing

        If retrievedData.TryGetData("ListViewItemFormat", item) Then
            Dim recreatedListViewItem As New ListViewItem(item)
            MessageBox.Show($"Data contains ListViewItem with text of {recreatedListViewItem.Text}")
        End If

    End If

    If retrievedData.GetDataPresent(GetType(Customer)) Then
        Dim newCustomer As Customer = Nothing

        If retrievedData.TryGetData(newCustomer) Then
            MessageBox.Show($"Data contains Customer with name of {newCustomer.Name}")
        End If

    End If

End Sub

The Customer class used in the previous snippet:

// Customer class used in custom clipboard format examples.
public class Customer
{
    public string Name { get; set; }
    
    public Customer(string name)
    {
        Name = name;
    }
}
'Customer class used in custom clipboard format examples.
Public Class Customer

    Public Property Name As String

    Public Sub New(ByVal name As String)
        Me.Name = name
    End Sub

End Class

See also