Files
docs-desktop/dotnet-desktop-guide/framework/winforms/advanced/how-to-retrieve-data-from-the-clipboard.md
T
Andy De George c0ba284473 Initial winforms content migrated (#18)
* Merge winforms framework content to working branch (#14)

* Breadcrumb / TOC / Move net5 to net folder (#15)

* change path from net5 to net

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Add .net 5 winforms placeholder article (#16)

* added some metadata and adjusted net5 placeholder

* corrections

* corrections

* corrections

* Test1

* Swapping landing page vs concept

* fix links

* Fix links

* Fix desc
2020-09-01 16:26:21 -07:00

5.2 KiB

title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title ms.date dev_langs helpviewer_keywords ms.assetid
How to: Retrieve Data from the Clipboard 03/30/2017
csharp
vb
pasting Clipboard data
Clipboard [Windows Forms], retrieving data
99612537-2c8a-449f-aab5-2b3b28d656e7

How to: Retrieve Data from the Clipboard

The xref:System.Windows.Forms.Clipboard class provides methods that you can use to 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 is also useful for transferring information from one application to another.

Some applications store data on the Clipboard in multiple formats to increase the number of other applications that can potentially use the data. A Clipboard format is a string that identifies the format. An application that uses the identified format can retrieve the associated data on the Clipboard. The xref:System.Windows.Forms.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. For information about adding data to the Clipboard, see How to: Add Data to the Clipboard.

To determine whether the Clipboard contains data in a particular format, use one of the ContainsFormat methods or the xref:System.Windows.Forms.Clipboard.GetData%2A method. To retrieve data from the Clipboard, use one of the GetFormat methods or the xref:System.Windows.Forms.Clipboard.GetData%2A method. These methods are new in .NET Framework 2.0.

To access data from the Clipboard by using versions earlier than .NET Framework 2.0, use the xref:System.Windows.Forms.Clipboard.GetDataObject%2A?displayProperty=nameWithType method and call the methods of the returned xref:System.Windows.Forms.IDataObject. To determine whether a particular format is available in the returned object, for example, call the xref:System.Windows.Forms.IDataObject.GetDataPresent%2A method.

Note

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

The xref:System.Windows.Forms.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 xref:System.STAThreadAttribute attribute.

To retrieve data from the Clipboard in a single, common format

  1. Use the xref:System.Windows.Forms.Clipboard.GetAudioStream%2A, xref:System.Windows.Forms.Clipboard.GetFileDropList%2A, xref:System.Windows.Forms.Clipboard.GetImage%2A, or xref:System.Windows.Forms.Clipboard.GetText%2A method. Optionally, use the corresponding ContainsFormat methods first to determine whether data is available in a particular format. These methods are available only in .NET Framework 2.0.

    [!code-csharpSystem.Windows.Forms.Clipboard#2] [!code-vbSystem.Windows.Forms.Clipboard#2]

To retrieve data from the Clipboard in a custom format

  1. Use the xref:System.Windows.Forms.Clipboard.GetData%2A method with a custom format name. This method is available only in .NET Framework 2.0.

    You can also use predefined format names with the xref:System.Windows.Forms.Clipboard.SetData%2A method. For more information, see xref:System.Windows.Forms.DataFormats.

    [!code-csharpSystem.Windows.Forms.Clipboard#3] [!code-vbSystem.Windows.Forms.Clipboard#3] [!code-csharpSystem.Windows.Forms.Clipboard#100] [!code-vbSystem.Windows.Forms.Clipboard#100]

To retrieve data from the Clipboard in multiple formats

  1. Use the xref:System.Windows.Forms.Clipboard.GetDataObject%2A method. You must use this method to retrieve data from the Clipboard on versions earlier than .NET Framework 2.0.

    [!code-csharpSystem.Windows.Forms.Clipboard#4] [!code-vbSystem.Windows.Forms.Clipboard#4] [!code-csharpSystem.Windows.Forms.Clipboard#100] [!code-vbSystem.Windows.Forms.Clipboard#100]

See also