是否有可能拦截控制台输出?(Is it possible to intercept Console output)

   IT问题网   2018-12-04 00:00:00

问 题

我所说的方法,比方说, fizzbuzz(),在这我管不着。使用这个方法输出一堆东西到控制台 console.writeline 。

是否有可能对我来说,拦截由 fizzbuzz 方法生成的输出? 请注意,我的应用程序是一个控制台应用程序本身。

解决方案

是的,很可能的:

var consoleout =新的stringwriter();
console.setout(consoleout);
console.writeline("这是截取。"); //这是不写入控制台
file.writealltext("consoleoutput.txt",consoleout.tostring());
 

后来,如果要停止截获控制台输出,使用如下修改:

var标准输出= console.out;
//上面的拦截code在这里..
console.setout(标准输出); //现在,所有的输出开始回到控制台窗口
 

还是 openstandardoutput 做,而不需要先保存标准流是相同的:

//上面的拦截code在这里..
var standardoutput =新的streamwriter(console.openstandardoutput());
standardoutput.autoflush = true;
console.setout(standardoutput); //现在,所有的输出开始回流到控制台
 

标签:是否是不是有可能拦截控制台输出



分享:

  • 微信
  • QQ好友
  • QQ空间
  • 新浪微博


热门推荐

当使用排序列表 TKEY的,TValue在一个SortedDictionary TKEY的,TValue?(When to use a SortedList<TKey, TValue> over a SortedDictionary<TKey, TValue>)

problem this may appear to be a duplicate of this questio ...

如何初始化列表 T给定的尺寸(相对于容量)?(How to initialize a List<T> to a given size (as opposed to capacity))

problem .net offers a generic list container whose perfor ...

如何使用XPath用的XDocument?(how to use XPath with XDocument)

problem there is a similar question, but it seems that th ...

什么是WebConfigurationManager和ConfigurationManager中的区别?(What's the difference between the WebConfigurationManager and the ConfigurationManager)

problem what's the difference between the webconfiguratio ...

NUnit的测试运行秩序(NUnit Test Run Order)

problem by default nunit tests run alphabetic ...

如何恢复从C#数据库(How to restore a database from C#)

problem i have a sql 2008 db. i am running a form that ba ...

调用堆栈不说"你来自哪里"但是"你要去哪里,下一个"?(The call stack does not say "where you came from", but "where you are going next")

problem in a previous question (get object ca ...

如何读取和写入ID3标签在C#中的MP3?(How to read and write ID3 tags to an MP3 in C#)

problem is there a library for reading and writing id3 ta ...

如何设置读取权限的X.509证书从.NET私钥文件(How to set read permission on the private key file of X.509 certificate from .NET)

problem here is the code to add a pfx to the cert store. ...

获取列表视图的项目双击事件(Get the item doubleclick event of listview)

problem how to get the item double click event of listvie ...

MongoDB的交易?(MongoDB transactions)

problem playing around with mongodb and norm in .net. th ...

确定是否.NET程序集是从相同的源代码构建(Determine whether .NET assemblies were built from the same source)

problem does anyone know of a way to compare two .net ass ...

如何prevent更新一个ListViewItem的的文字时,在闪烁的ListView?(How to prevent flickering in ListView when updating a single ListViewItem's text)

problem all i want is to update an listviewit ...

谁应该调用Dispose IDisposable的对象时传递到另一个对象?(Who should call Dispose on IDisposable objects when passed into another object)

problem is there any guidance or best practic ...

获得一天的后缀使用则DateTime.ToString时,()(Getting day suffix when using DateTime.ToString())

problem is it possible to include the day suffix when for ...

改变目标框架我在Visual Studio解决方案的所有项目(Change the Target Framework for all my projects in a Visual Studio Solution)

problem i need to change the target framework ...

string.IsNullOrEmpty(串)与string.IsNullOrWhiteSpace(串)(string.IsNullOrEmpty(string) vs. string.IsNullOrWhiteSpace(string))

problem is use of string.isnullorempty(string ...

上传文件使用C#到ftp(Upload file to ftp using c#)

problem i try upload a file to an ftp-server with c#. the ...

什么是App.config中呢?(What is app.config for)

problem settings.settings generates settings.designer.cs ...

我可以使用ASP.NET MVC与常规ASP.NET Web表单(Can I use ASP.NET MVC together with regular ASP.NET Web forms)

problem i have on request from a client built a huge site ...