user1823701 user1823701 - 2 months ago 22
C# Question

Error while trying to copy string to clipboard

I have tried this code:

Clipboard.SetText("Test!");


And I get this error:


Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your
Main
function has
STAThreadAttribute
marked on it.


How can I fix it?

Answer

Put [STAThread] above your main method:

[STAThread]
static void Main()
{
}
Comments