我想获取当前存储在 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);
}
}
关于c# - 从那里获取数据然后返回到 Windows 剪贴板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14635599/