如果我在字符串中有一些文本,我如何将其复制到剪贴板,以便用户可以将其粘贴到另一个窗口(例如,从我的应用程序到记事本)?
System.Windows.Clipboard (PresentationCore.dll)
Winforms: System.Windows.Forms.Clipboard
两者都有一个静态的SetText方法。
System.Windows.Forms.Clipboard.SetText (Windows窗体)或System.Windows.Clipboard.SetText (WPF)
I wish calling SetText were that easy but there are quite a few gotchas that you have to deal with. You have to make sure that the thread you are calling it on is running in the STA. It can sometimes fail with an access denied error then work seconds later without problem - something to do with the COM timing issues in the clipboard. And if your application is accessed over Remote Desktop access to the clipboard is sketchy. We use a centralized method to handle all theses scenarios instead of calling SetText directly.
以下是我们的集中代码:
StaHelper类只是在单线程单元(STA)中的线程上执行一些任意代码——这是剪贴板所需要的。
abstract class StaHelper
{
readonly ManualResetEvent _complete = new ManualResetEvent( false );
public void Go()
{
var thread = new Thread( new ThreadStart( DoWork ) )
{
IsBackground = true,
}
thread.SetApartmentState( ApartmentState.STA );
thread.Start();
}
// Thread entry method
private void DoWork()
{
try
{
_complete.Reset();
Work();
}
catch( Exception ex )
{
if( DontRetryWorkOnFailed )
throw;
else
{
try
{
Thread.Sleep( 1000 );
Work();
}
catch
{
// ex from first exception
LogAndShowMessage( ex );
}
}
}
finally
{
_complete.Set();
}
}
public bool DontRetryWorkOnFailed{ get; set; }
// Implemented in base class to do actual work.
protected abstract void Work();
}
然后我们有一个特定的类来设置剪贴板上的文本。在某些Windows/. net版本的某些边缘情况下,需要手动创建数据对象。我现在不记得具体的场景了,在。net 3.5中可能不需要它。
class SetClipboardHelper : StaHelper
{
readonly string _format;
readonly object _data;
public SetClipboardHelper( string format, object data )
{
_format = format;
_data = data;
}
protected override void Work()
{
var obj = new System.Windows.Forms.DataObject(
_format,
_data
);
Clipboard.SetDataObject( obj, true );
}
}
用法如下:
new SetClipboardHelper( DataFormats.Text, "See, I'm on the clipboard" ).Go();
在Windows窗体中,如果你的字符串在文本框中,你可以很容易地使用这个:
textBoxcsharp.SelectAll();
textBoxcsharp.Copy();
textBoxcsharp.DeselectAll();
使用这个问题中显示的解决方案System.Windows.Forms.Clipboard.SetText(…),会导致异常:
在OLE调用之前,当前线程必须设置为单线程公寓(STA)模式
为了防止这种情况,你可以添加属性:
[STAThread]
to
static void Main(string[] args)
这对我来说很管用:
你想做的是:
System.Windows.Forms.Clipboard.SetText("String to be copied to Clipboard");
但是它会导致一个错误,说它必须在ApartmentState.STA的单个线程中。
让我们让它在这样一个线程中运行。它的代码:
public void somethingToRunInThread()
{
System.Windows.Forms.Clipboard.SetText("String to be copied to Clipboard");
}
protected void copy_to_clipboard()
{
Thread clipboardThread = new Thread(somethingToRunInThread);
clipboardThread.SetApartmentState(ApartmentState.STA);
clipboardThread.IsBackground = false;
clipboardThread.Start();
}
在调用copy_to_clipboard()之后,字符串被复制到剪贴板中,因此您可以使用Paste或Ctrl + V并将字符串作为string返回,以复制到剪贴板中。
使用try-catch,即使它有错误,它仍然会复制。
Try
Clipboard.SetText("copy me to clipboard")
Catch ex As Exception
End Try
如果使用消息框捕获异常,它将显示错误,但值仍然复制到剪贴板。
推荐文章
- 实体框架核心:在上一个操作完成之前,在此上下文中开始的第二个操作
- 如何为构造函数定制Visual Studio的私有字段生成快捷方式?
- 为什么Visual Studio 2015/2017/2019测试运行器没有发现我的xUnit v2测试
- 如何使用JSON确保字符串是有效的JSON。网
- AppSettings从.config文件中获取值
- 通过HttpClient向REST API发布一个空体
- 如何检查IEnumerable是否为空或空?
- 自动化invokerrequired代码模式
- 没有ListBox。SelectionMode="None",是否有其他方法禁用列表框中的选择?
- 在c#代码中设置WPF文本框的背景颜色
- 在c#中,什么是单子?
- c#和Java中的泛型有什么不同?和模板在c++ ?
- c#线程安全快速(est)计数器
- 如何将此foreach代码转换为Parallel.ForEach?
- 如何在iis7应用程序池中设置。net Framework 4.5版本