Clipboard.GetData Method
Retrieves data in a specified format from the Clipboard.
Assembly: PresentationCore (in PresentationCore.dll)
Parameters
- format
- Type: System.String
A string that specifies the format of the data to retrieve. For a set of predefined data formats, see the DataFormats class.
Return Value
Type: System.ObjectAn object that contains the data in the specified format, or null if the data is unavailable in the specified format.
| Exception | Condition |
|---|---|
| ArgumentNullException |
format is null. |
The following example demonstrates the use of this method.
// After this line executes, IsHTMLDataOnClipboard will be true if
// HTML data is available natively on the clipboard; if not, it
// will be false.
bool IsHTMLDataOnClipboard = Clipboard.ContainsData(DataFormats.Html);
// If there is HTML data on the clipboard, retrieve it.
string htmlData;
if(IsHTMLDataOnClipboard)
{
htmlData = Clipboard.GetText(TextDataFormat.Html);
}
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Call to Clipboard.GetData("PreferredDropEffect:): Corrupt Memory Problem.
I was experiencing a "Corrupt Memory" abort in an (Explorer-like) application I am developing. The program would abort at different places, so it was hard to find, but it always happened at some point after the user had performed a Copy (or Cut) followed by a Paste operation. Through trial and error I finally determined that if I commented out the following code (intended to determine whether the user had filled the clipboard with a "Cut" versus a "Copy") the program would not fail. As soon as I put it back in, it would (eventually) abort. When I traced the code below I noticed the first time it was called, all variables looked reasonable, but the subsequent calls returned garbage. I tried repositioning the stream after reading it and tried various forms of "Stream" to read the data... no joy. After countless permutations of the code below, I gave up and resorted to using P/Invoke to get the job done and that fixed my immediate problem. Since my workaround "solved" the problem I am no longer in a hurry to fix it, but thought I would share this in case anyone else was having similar problems. A search of the internet found one other developer out there that had the same problems Would be interested in any feedback. Here is the link I found to his description of the same problem back in 2006: http://objectmix.com/csharp/118519-reading-clipboard-data-crashes-explorer.html
if (dataObject.GetDataPresent("Preferred DropEffect") == true) {
System.IO.Stream strm = (System.IO.Stream)Clipboard.GetData("Preferred DropEffect");
object data = dataObject.GetData("Preferred DropEffect");
if (data is System.IO.Stream) {
Stream stream = (Stream)data;
StreamReader reader = new StreamReader(stream);
int value = reader.Read();
stream.Position = 0; // This had no apparent effect
if ((value & 2) == 2) {
dragDropEffect = DragDropEffects.Move;
}
else {
dragDropEffect = dragDropEffect = DragDropEffects.Copy;
}
}
}
if (dataObject.GetDataPresent("Preferred DropEffect") == true) {
System.IO.Stream strm = (System.IO.Stream)Clipboard.GetData("Preferred DropEffect");
object data = dataObject.GetData("Preferred DropEffect");
if (data is System.IO.Stream) {
Stream stream = (Stream)data;
StreamReader reader = new StreamReader(stream);
int value = reader.Read();
stream.Position = 0; // This had no apparent effect
if ((value & 2) == 2) {
dragDropEffect = DragDropEffects.Move;
}
else {
dragDropEffect = dragDropEffect = DragDropEffects.Copy;
}
}
}
- 9/19/2011
- CW Harmon