【发布时间】:2017-04-09 18:30:00
【问题描述】:
我在互联网上搜索了很多内容以将控制台应用程序输出屏幕存储在 .txt 文件中。我得到了解决方案,但没有得到想要的结果。问题是控制台屏幕中未显示的文本存储在记事本文件中。但是控制台屏幕中显示的文本并没有存储在记事本文件中。
例如,
using System;
using System.IO;
static void Main(string[] args)
{
Console.WriteLine("Text which is displayed in the the console output screen ");
FileStream filestream = new FileStream("notepad.txt", FileMode.Create);
var streamwriter = new StreamWriter(filestream);
streamwriter.AutoFlush = true;
Console.SetOut(streamwriter);
Console.SetError(streamwriter);
Console.WriteLine("Text which is not displayed in the console output screen but it store in the the .txt file");
Console.ReadKey();
}
在上面的例子中Console.WriteLine("Text which is displayed in the the console output screen "); 行仅显示在控制台屏幕中,但未存储在 记事本 文件中
但Console.WriteLine("Text which is not displayed in the console output screen but it store in the the .txt file"); 行没有显示在控制台应用程序屏幕中,而是存储在记事本文件中。
我需要存储控制台屏幕上显示的所有内容,即使用户也提供了详细信息。
我该怎么做?
由于我是初学者,我希望答案很简单。
提前致谢!
【问题讨论】:
-
只是您的示例的旁注 -
FileStream和StreamWriter实现IDisposable接口,因此应该将它们处理掉。例如using(FileStream filestream = new FileStream("notepad.txt", FileMode.Create)){ /* use filestream */ ... }您可以在 msdn.microsoft.com/en-us/library/b1yfkh5e(v=vs.110).aspx 了解更多关于 Dispose 模式的信息。
标签: c# console-application notepad