Treat the clipboard like a TextReader and TextWriter
PM> Install-Package MasterDevs.Clippy
We use System.Windows.Clipboard to read and write to the clipboard.
Since this is an OLE object it needs to be accessed from a STAThread.
If you are in a WPF application, you shouldn't need to worry about this.
If you are in a console application, you will need to make sure that you are using a STAThread.
The easiest way is to decorate your startup method with the STAThreadAttribute like so:
[STAThread]privatestaticintMain(string[]args){// ...}To read from the clipboard, simply create a new ClipboardReader
using(varclippy=newClipboardReader()){varclipboardText=clippy.ReadToEnd();}To write to the clipboard, simply create a ClipboardWriter
using(varclippy=newClipboardWriter()){clippy.Write("Hello World");}