简体   繁体   English

C#复制到剪贴板在运行时不起作用

[英]c# copy to clipboard not working in running time

I'm working on a web application using asp.net and c# in the code behind. 我正在使用asp.net和c#在后面的代码中的Web应用程序上。 I just want to copy a text in a textbox to clipboard, my code is as follow: 我只想将文本框中的文本复制到剪贴板,我的代码如下:

The problem is that this is working properly in debug mode, but when deploying my website, in running mode the CopyToClipboard is not working! 问题是,这在调试模式下可以正常工作,但是在部署我的网站时,在运行模式下,CopyToClipboard无法正常工作!

Thread newThread = new Thread(new ThreadStart(CopyToClipboard));
newThread.SetApartmentState(ApartmentState.STA);
newThread.Start();

private void CopyToClipboard()
{
    Clipboard.Clear();
    Clipboard.SetData("Text", txtEmailId.Text);
    Clipboard.SetText(txtEmailId.Text);
}

That Clipboard class resides in System.Windows.Forms. Clipboard类驻留在System.Windows.Forms中。 You cannot use this in ASP.net, because it will only attempt to modify the clipboard state of the server the ASP.net process runs on. 您不能在ASP.net中使用它,因为它只会尝试修改运行ASP.net进程的服务器的剪贴板状态。 And that's probably not what you're looking for. 而这可能不是您想要的。

To modify the clipboard from a website, you need to look at unreliable Javascript solutions. 要从网站修改剪贴板,您需要查看不可靠的Javascript解决方案。 Please note that the clipboard is potentially unsafe and possible can't be accessed at all. 请注意,剪贴板可能不安全,可能根本无法访问。 Also see How do I copy to the clipboard in JavaScript? 另请参阅如何使用JavaScript复制到剪贴板?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM