如何使用c#将控制台应用程序中运行的内容保存到text / doc文件 [英] how to save the contents running in a console application to a text/doc file using c#

查看:335
本文介绍了如何使用c#将控制台应用程序中运行的内容保存到text / doc文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用c#将我的控制台应用程序窗口中运行的内容保存到text / doc文件。

 Console.WriteLine(); 
// 这里我的实际输出窗口

< span class =code-comment> // 尝试将输出保存到D盘中的文本文件,来自Console.WriteLine();

FileStream fs = new FileStream( D:\\ newlist.txt,FileMode.Create);
// 首先,保存标准输出。
TextWriter tmp = Console。出;
StreamWriter sw = new StreamWriter(fs);
Console.SetOut(sw);
sw.Close();



我尝试过这段代码,但是它提供了没有内容的空文本文件。如何使用c#将我的实际输出保存到text / doc文件。

请协助



提前致谢

解决方案

1。将控制台重定向到您的文件(最多为Console.SetOut(sw))

2.完成所有文字(评论中的代码)

3.关闭所有文件...



请参阅此处的示例以了解操作的顺序...... https://msdn.microsoft.com/en-us/library/system.console.setout(v = vs.110)的.aspx [ ^ ]

I want to save the contents running in a my console application window to a text/doc file using c#.

Console.WriteLine();
 //Here i got my actual output window 
                             
 //trying to save my output to a text file in D drive from Console.WriteLine();

FileStream fs = new FileStream("D:\\newlist.txt",  FileMode.Create);
                                // First, save the standard output.
                                TextWriter tmp = Console.Out;
                                StreamWriter sw = new StreamWriter(fs);
                                Console.SetOut(sw);
                                sw.Close();


I have tried with this code , but it gives empty text file with no content. how to save my actual output to a text/doc file using c#.
Please assist

Thanks in advance

解决方案

1. Redirect the console to your file (up to Console.SetOut(sw))
2. Do all your writings (the code from your comment)
3. Close all down...

See the sample here to understand the order of actions... https://msdn.microsoft.com/en-us/library/system.console.setout(v=vs.110).aspx[^]


这篇关于如何使用c#将控制台应用程序中运行的内容保存到text / doc文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆