如何从控制台获取所有文本到字符串?

时间:2014-08-11 10:40:42

标签: c# nunit

我以这种方式进行测试:

NUnit.ConsoleRunner.Runner.Main(new string[]
    {
        System.Reflection.Assembly.GetExecutingAssembly().Location,"OpenShop_Firefox.dll",                   
    });

我希望从控制台获取所有文本到一个字符串。什么是最好的方式?

1 个答案:

答案 0 :(得分:2)

您需要将Console.Out设置为您选择的流:

using (StringWriter stringWriter = new StringWriter())
{
    Console.SetOut(stringWriter);

    NUnit.ConsoleRunner.Runner.Main(new string[]
    {
        System.Reflection.Assembly.GetExecutingAssembly().Location,
        "OpenShop_Firefox.dll"
    });

    string allConsoleOutput = stringWriter.ToString();
}