开发者

How can I copy text to the clipboard from a Mono/C# console application on OS X?

开发者 https://www.devze.com 2023-04-03 17:11 出处:网络
I\'m writing a console app in C#, using Mono on OS X. On Windows, here is how I copy text to the clipboard:

I'm writing a console app in C#, using Mono on OS X.

On Windows, here is how I copy text to the clipboard:

Clipboard.SetText("text");

...from a method marked up with the STAThread attribute:

[STAThread] // for OLE
public static vo开发者_运维问答id Main (string[] args)
{
    Clipboard.SetText("text");
}

Clipboard is defined in System.Windows.Forms. That code compiles and runs both under the official Microsoft .NET runtime on Windows and on the the Mono runtime on OS X.

On Windows, the text gets copied, so it's not an issue of forgetting a reference or a using statement.

Unfortunately, when I run the code under Mono/OS X, the text isn't actually copied to the Clipboard. Something appears quickly on my Dock and then disappears just as quickly, but either way, the text I'm trying to copy doesn't end up on the clipboard.

So: how do I copy text do the clipboard on OS X (and Linux?) using Mono?

If you're interested in seeing the entire project, the code is here: https://github.com/leeohsheeus/boom-sharp.


If you want to program for multiple platforms using Mono, Gtk.Clipboard might be interesting for you.

Minimum Usage-Example:

Gtk.Clipboard clipboard = Gtk.Clipboard.Get(Gdk.Atom.Intern("CLIPBOARD", false));

clipboard.Text = "Hello World";

Also this Stackoverflow-Question might be interesting.


Console apps themselves cannot copy text to a clipboard. The user will have to use Copy&Paste from the terminal application.

0

精彩评论

暂无评论...
验证码 换一张
取 消