如何获得C#控制台的打印信息

如题所述

有两方法

方法1:

用代码编程实现

using System.IO;
……

// 打开写入流
StreamWriter sw = new StreamWriter(@"F:\Test\ConsoleOutput.txt");      
// 将Console的输出定向到文本文件F:\Test\ConsoleOutput.txt
Console.SetOut(sw);      
      
// Console的输出全部写入刚才的文本文件      
Console.WriteLine("Here is the result:");      
Console.WriteLine("Processing......");      
Console.WriteLine("OK!");      

// 关闭文件
sw.Flush();      
sw.Close();


方法2:

不用方法一的代码,直接在命令窗口中使用重定向

假设你编译好的控制台程序为d:\ConsoleApplication1.exe,在命令窗口中输入

d:\> ConsoleApplication1 > ConsoleOutput.txt

控制台的输出将被写入文本文件ConsoleOutput.txt

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答