Ale*_*lex 37
您需要引用命名空间:
using System.Windows.Forms;
Run Code Online (Sandbox Code Playgroud)
然后你可以使用:
Clipboard.SetText("Whatever you like");
Run Code Online (Sandbox Code Playgroud)
编辑
这是一个适合我的复制和粘贴解决方案
using System;
using System.Windows.Forms;
namespace ConsoleApplication1
{
class Program
{
[STAThread]
private static void Main(string[] args)
{
Console.WriteLine("Say something and it will be copied to the clipboard");
var something = Console.ReadLine();
Clipboard.SetText(something);
Console.Read();
}
}
}
Run Code Online (Sandbox Code Playgroud)
Ale*_*ker 14
使用
System.Windows.Forms.Clipboard.SetText(message)
Run Code Online (Sandbox Code Playgroud)
其中message是要复制的字符串.
尽管System.Windows.Forms命名空间是为Windows窗体设计的,但其API中的许多方法甚至在控制台/其他非Winforms应用程序中也具有重要用途.
1:需要添加引用System.Windows.Forms如下:
右键单击您的项目并Solution Explorer选择Add reference...,然后找到System.Windows.Forms并添加它。(看看这个答案)
2:然后您可以System.Windows.Forms使用下面的行添加到您的代码中,并确保将其正确放置在与其他using代码应放置的位置:
using System.Windows.Forms;
Run Code Online (Sandbox Code Playgroud)
3:添加[STAThread]在你的函数的顶部Main,所以它应该是这样的:
[STAThread]
static void Main(string[] args)
{
....
}
Run Code Online (Sandbox Code Playgroud)
4:Clipboard随意使用,例如:
Clipboard.SetText("Sample Text");
Run Code Online (Sandbox Code Playgroud)