Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get data from then back to Windows Clipboard

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?

like image 837
user1472066 Avatar asked Jan 31 '13 21:01

user1472066


People also ask

How do I retrieve previously copied text in Windows?

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.


2 Answers

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);
          }
    }
like image 51
Axel Kemper Avatar answered Sep 18 '22 00:09

Axel Kemper


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);
        }
like image 39
sa_ddam213 Avatar answered Sep 21 '22 00:09

sa_ddam213