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?
To get to your clipboard history, press Windows logo key + V. From the clipboard history, you can paste and pin frequently used items by choosing an individual item from your clipboard menu. Pinning an item keeps it from being removed from the clipboard history to make room for new items.
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);
}
}
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With