case
ADM Blog
1Mar/100

Use clipboard (copy/paste) in C# console application

net_logo.jpgThis took me few minutes to figure out and was quite annoying. First you must add a reference to System.Windows.Forms in your application. Go to Project -> Add reference, select System.Windows.Forms from .NET tab in the window that just opened.  You must avoid the ThreadStateException by applying the STAThread attribute to your Main() function. Then you can use the Clipboard functions without any problems.

1
2
3
4
5
6
7
8
9
using System;
using System.Windows.Forms;
 
class Program {
    [STAThread]
    static void Main(string[] args) {
         Clipboard.SetText("this is in clipboard now");
    }
}
Comments (0) Trackbacks (0)
  1. Thanks!

  2. Great, thank you. The [STAThread] was what I was missing.

  3. How cone in mvs, you have to add a reference and not just type it in?


Reply

( Cancel )

CommentLuv badge

*

No trackbacks yet.