How to get all text from console to string?

*爱你&永不变心* 提交于 2021-01-28 01:07:00

问题


I do test in that way:

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?


回答1:


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


来源:https://stackoverflow.com/questions/25241323/how-to-get-all-text-from-console-to-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!