c# – 从中​​获取数据然后返回Windows剪贴板

我想获取当前存储在Windows剪贴板中的数据并将其保存在变量中,然后将数据放回剪贴板.

现在我正在使用这段代码:

object l_oClipBrdData = Clipboard.GetDataObject();
Clipboard.SetDataObject(l_oClipBrdData ,true);

但在这之后,剪贴板是空的.

我究竟做错了什么?

解决方法:

这是一个演示’剪贴板’对象的示例:

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);
          }
    }
上一篇:linux – 使用vala将uris插入Gtk.Clipboard


下一篇:clipboard兼容ios和安卓的复制插件