在编写控制台应用程序时,有没有一种方法可以创建第二个控制台来输出?
你可以启动一个新的cmd.exe进程,并使用stdio和stdout发送和接收数据。
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();
在 MSDN 上有更多信息。
//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;
}
}
CONSOLE_NAME
是在哪里和如何定义的? - Dai一个控制台只能附加到任何给定的进程。所以简而言之,你不能。但是有办法“假装它”。