Is there a way to create a second console to output to in .NET when writing a console application?

有些话、适合烂在心里 提交于 2019-12-28 05:34:34

问题


Is there a way to create a second console to output to in .NET when writing a console application?


回答1:


Well, you could start a new cmd.exe process and use stdio and stdout to send and recieve data.

ProcessStartInfo psi = new ProcessStartInfo("cmd.exe")
{
    RedirectStandardError = true,
    RedirectStandardInput = true,
    RedirectStandardOutput = true,
    UseShellExecute = false
};

Process p = Process.Start(psi);

StreamWriter sw = p.StandardInput;
StreamReader sr = p.StandardOutput;

sw.WriteLine("Hello world!");
sr.Close();

More info on MSDN.




回答2:


The following fires off an application-dependent number of console windows and stores the amount and parameters for the console inside a String Dictionary that is then looped to generate the required amount of spawned console apps. You would only need the process stuff if only spawning one of course.

//Start looping dic recs and firing console
foreach (DictionaryEntry tests in steps)
{
    try
    {
        Process runCmd = new Process();
        runCmd.StartInfo.FileName = CONSOLE_NAME;
        runCmd.StartInfo.UseShellExecute = true;
        runCmd.StartInfo.RedirectStandardOutput = false;
        runCmd.StartInfo.Arguments = tests.Value.ToString();

        if (cbShowConsole.Checked)
        {
            runCmd.StartInfo.CreateNoWindow = true;
            runCmd.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
        }
        else
        {
            runCmd.StartInfo.CreateNoWindow = false;
            runCmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; 
        }
        runCmd.Start();                
    }
    catch (Exception ex)
    {
        string t1 = ex.Message;
    }
}

Note this is intended either to run hidden (CreateNoWindow) or visible.




回答3:


A single console is attached to any given process. So in short you can not. But there are ways to "fake it"



来源:https://stackoverflow.com/questions/697227/is-there-a-way-to-create-a-second-console-to-output-to-in-net-when-writing-a-co

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