解决方法
下面的一个基本解决方案(只是将标准输出重定向到StringBuilder实例).
您可能需要在控制台应用程序中自己添加对System. Windows.Forms的引用.
您可能需要在控制台应用程序中自己添加对System. Windows.Forms的引用.
using System; using System.IO; using System.Text; using System.Windows.Forms; public class Redirect { [STAThread()] public static void Main() { StringBuilder sb = new StringBuilder(); StringWriter sw = new StringWriter(sb); Console.SetOut(sw); // redirect Console.WriteLine("We are redirecting standard output now..."); for (int i = 0; i < 10; i++) { Console.WriteLine(i); } sw.Close(); StringReader sr = new StringReader(sb.ToString()); string completeString = sr.ReadToEnd(); sr.Close(); Clipboard.SetText(sb.ToString()); Console.ReadKey(); // just wait... (press ctrl+v afterwards) } }