如何从控制台获取所有文本到字符串? [英] How to get all text from console to string?

查看:63
本文介绍了如何从控制台获取所有文本到字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我就是这样测试的:

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

我想将所有文本从控制台获取到一个字符串.最好的方法是什么?

And i want to get all text from console to one string. What is the best way?

推荐答案

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

You need to set Console.Out to a stream of your choosing:

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();
}

这篇关于如何从控制台获取所有文本到字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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