Get data from then back to Windows Clipboard
Solution 1
Here is an example to demonstrate the 'Clipboard' object:
string text;
string[] a;
if (Clipboard.ContainsText())
{
text = Clipboard.GetText(TextDataFormat.Text);
// the following could have been done simpler with
// a Regex, but the regular expression would be not
// exactly simple
if (text.Length > 1)
{
// unify all line breaks to \r
text = text.Replace("\r\n", "\r").Replace("\n", "\r");
// create an array of lines
a = text.Split('\r');
// join all trimmed lines with a space as separator
text = "";
// can't use string.Join() with a Trim() of all fragments
foreach (string t in a)
{
if (text.Length > 0)
text += " ";
text += t.Trim();
}
Clipboard.SetDataObject(text, true);
}
}
Solution 2
Clipboard.GetDataObject()
will return the IDataObject
from the clipboard, if you want to get the actual data you can call GetData(typeof(dataType))
Example:
int mydata = 100;
Clipboard.SetDataObject(mydata, true);
var clipData = Clipboard.GetDataObject().GetData(typeof(int));
There are also a lot of predifined dataTypes you can use
Example:
if (Clipboard.ContainsData(DataFormats.Bitmap))
{
var clipData = Clipboard.GetData(DataFormats.Bitmap);
}
Solution 3
Object that you pass to SetDataObject() should support serialization. If this is your own type, mark it with [Serializable] attribute.
More info:
http://msdn.microsoft.com/en-gb/library/cs5ebdfz(v=vs.90).aspx
http://www.codeproject.com/Articles/8102/Saving-and-obtaining-custom-objects-to-from-Window
user1472066
Updated on July 03, 2022Comments
-
user1472066 almost 2 years
I would like to get the data currently stored in the Windows Clipboard and save it in a variable, then put the data back into the clipboard.
Right now I'm using this code:
object l_oClipBrdData = Clipboard.GetDataObject(); Clipboard.SetDataObject(l_oClipBrdData ,true);
But after doing that the clipboard is empty.
What am I doing wrong?
-
Leon Newswanger about 11 yearsAre you getting any exceptions?
-
David Heffernan about 11 yearsWhat is on the clipboard when this code runs? Also, why are you using object? It's IDataObject.
-
Andrew almost 6 years
-
-
David Heffernan about 11 yearsThe object is pulled off the clipboard.
-
David Heffernan about 11 yearsWhy are you concentrating on text. Question seems general. Why does the code in the Q not behave as the asker expects? If you are putting text on the clipboard, use SetText.
-
user1472066 about 11 yearsThere are no exceptions and its not my object, its an object that is already in the clipboard. it could be anything text, a printscreen a file or anything else.. I changed it to IdataObject - still doesn't work
-
Axel Kemper about 11 yearsThe example was meant to demonstrate the steps: 1.) make sure that the Clipboard contains data of the type you expect/want 2.) copy the data 3.) process the data 4.) Write it back. For its own sake, the sample is useful. I call it several times a day via taskbar shortcut to convert texts with linebreaks to strings suitable as filenames.
-
user1472066 about 11 yearsI succeeded doing so with text.. but that is not enough. What I want to do is create a clipboard that can store more that 1 item so I could copy and paste things as I please without overriding the copied data everytime I press CTRL+C. the code works perfectly if I'm using setText and getText.. but I want it to be able to work for everything that may be stored in the clipboard. so I want to take whatever is in the clipboard, store it, and then put it back exactly the way it was
-
Axel Kemper about 11 yearsAnother Clipboard manager (in C#) was proposed in Codeproject a while back: codeproject.com/Articles/18568/…
-
IS4 almost 9 yearsThere is no
Flush
inClipboard
.