当我的应用程序试图在控制台上写入内容时,我需要捕获该事件。如何在事件或方法中将文本发送到控制台输出
Console.WriteLine("Any text");
是否可以将文本发送到事件或方法的控制台输出?
当我的应用程序试图在控制台上写入内容时,我需要捕获该事件。如何在事件或方法中将文本发送到控制台输出
Console.WriteLine("Any text");
是否可以将文本发送到事件或方法的控制台输出?
一种方法是创建一个新的流控,如本文所示:
http://mel-green.com/2010/01/progressstream/
然后,你需要设置这是该数据流控制台写入,例如
MemoryStream ms = new MemoryStream();
ProgressStream progressStream = new ProgressStream(ms);
Console.SetOut(new StreamWriter(progressStream));
然后使用进度流的事件来查看它的写入时间。
其实有第三方引用。他们只是使用Console.WriteLine。我已经包含这些DLL。所以我不能应用这个解决方案,因为我不能修改它们。 –
第三方参考?我什么也没有看到,所有的来源都在那里。我不确定你的意思。 –
让我试试解决方案。我认为它应该适合我。 –
这可能对你有帮助:
using System;
using System.IO;
namespace nsStreams
{
public class Redirect
{
static public void Main()
{
FileStream ostrm;
StreamWriter writer;
TextWriter oldOut = Console.Out;
try
{
ostrm = new FileStream ("./Target.txt", FileMode.OpenOrCreate, FileAccess.Write);
writer = new StreamWriter (ostrm);
}
catch (Exception e)
{
Console.WriteLine (e.Message);
return;
}
Console.SetOut (writer);
Console.SetOut (oldOut);
writer.Close();
ostrm.Close();
Console.WriteLine ("Done");
}
}
}
您可以[重定向'Console.Out'(http://stackoverflow.com/questions/6024172/is-it-possible-to-intercept -console-output) – PHeiberg
在这里,试试这个,有些类似于你的要求。 http://stackoverflow.com/questions/11911660/redirect-console-writeline-from-windows-application-to-a-string –