如何复制数据到剪贴板在c#

我怎么能复制一个字符串(例如“hello”)到系统剪贴板在c#,所以下次我按CTRL + V我会得到“hello”?

472844 次浏览

有两个类位于不同的程序集和不同的名称空间中。

  • WinForms:使用以下命名空间声明,确保Main[STAThread]属性标记:

    using System.Windows.Forms;
    
  • WPF: use following namespace declaration

    using System.Windows;
    
  • console: add reference to System.Windows.Forms, use following namespace declaration, make sure Main is marked with [STAThread] attribute. Step-by-step guide in another answer

    using System.Windows.Forms;
    

To copy an exact string (literal in this case):

Clipboard.SetText("Hello, clipboard");

要复制文本框的内容,可以使用TextBox.Copy ()或先获取文本,然后设置剪贴板值:

Clipboard.SetText(txtClipboard.Text);
< p > # EYZ0。 还是……MSDN官方文档这里是WPF.


备注:

Clipboard.SetText("hello");

为此,您需要使用System.Windows.FormsSystem.Windows名称空间。

我在这个问题上使用WPF c#剪贴板和System.Threading.ThreadStateException的经验是我的代码,在所有浏览器中都正确工作:

Thread thread = new Thread(() => Clipboard.SetText("String to be copied to clipboard"));
thread.SetApartmentState(ApartmentState.STA); //Set the thread to STA
thread.Start();
thread.Join();

感谢这篇文章在这里

但这只在本地主机上工作,所以不要在服务器上尝试这一点,因为它不会工作。

在服务器端,我使用zeroclipboard来实现。唯一的办法,经过大量研究。

对于循序渐进的控制台项目,您必须首先添加System.Windows.Forms引用。以下步骤在Visual Studio Community 2013和。net 4.5中工作:

  1. 解决方案资源管理器中,展开控制台项目。
  2. 右键单击参考文献,然后单击添加引用…
  3. 程序集组中,在框架下,选择System.Windows.Forms
  4. 单击# EYZ0。

然后,将下面的using语句添加到代码顶部的其他语句中:

using System.Windows.Forms;

然后,添加下列Clipboard中的任意一个。SetText语句:

Clipboard.SetText("hello");
// OR
Clipboard.SetText(helloString);

最后,添加STAThreadAttribute到你的Main方法,如下所示,以避免System.Threading.ThreadStateException:

[STAThreadAttribute]
static void Main(string[] args)
{
// ...
}

Clip.exe是Windows下用于设置剪贴板的可执行文件。请注意,这并不适用于其他操作系统,除了Windows,这仍然很糟糕。

        /// <summary>
/// Sets clipboard to value.
/// </summary>
/// <param name="value">String to set the clipboard to.</param>
public static void SetClipboard(string value)
{
if (value == null)
throw new ArgumentNullException("Attempt to set clipboard with null");


Process clipboardExecutable = new Process();
clipboardExecutable.StartInfo = new ProcessStartInfo // Creates the process
{
RedirectStandardInput = true,
FileName = @"clip",
};
clipboardExecutable.Start();


clipboardExecutable.StandardInput.Write(value); // CLIP uses STDIN as input.
// When we are done writing all the string, close it so clip doesn't wait and get stuck
clipboardExecutable.StandardInput.Close();


return;
}
在ASP.net web表单中使用@page AspCompat="true",将system.windows.forms添加到您的项目中。 在你的网上。配置添加:< / p >
  <appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="false" />
</appSettings>

然后你可以使用:

Clipboard.SetText(CreateDescription());

如果你不想将线程设置为STAThread,使用Clipboard.SetDataObject(object sthhere):

Clipboard.SetDataObject("Yay! No more STA thread!");

这在。net core上工作,不需要引用System.Windows.Forms

using Windows.ApplicationModel.DataTransfer;


DataPackage package = new DataPackage();
package.SetText("text to copy");
Clipboard.SetContent(package);

它是跨平台的。在windows中,您可以按窗户 + V查看剪贴板历史记录

如果你不想或者不能使用System.Windows.Forms,你可以使用Windows本地api: user32和剪贴板函数GetClipboardDataSetClipboardDat (pinvoke)

.NET 6包装器库可以在https://github.com/MrM40/WitWinClipboard/tree/main中找到