Get data from then back to Windows Clipboard

c#, clipboard, copy, paste

Solution

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);
          }
    }

Problem

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?

Original source