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]
private static int Main(string[] args)
{
// ...
}
To read from the clipboard, simply create a new ClipboardReader
using (var clippy = new ClipboardReader())
{
var clipboardText = clippy.ReadToEnd();
}
To write to the clipboard, simply create a ClipboardWriter
using (var clippy = new ClipboardWriter())
{
clippy.Write("Hello World");
}