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 hasSTAThreadAttribute
marked on it.
How can I fix it?
You need to call that method specially, because it uses some legacy code. Try this:
You can only access the clipboard from an STAThread.
The quickest way to solve this is to put
[STAThread]
on top of yourMain()
method, but if for whatever reason you cannot do that you can use a separate class that creates an STAThread set/get the string value for to you.Put
[STAThread]
above your main method: