我正在使用 .NET 4 控制台应用程序中的以下代码:
private static void AttachToConsole ()
{
System.Diagnostics.Process process = null;
process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = false;
process.EnableRaisingEvents = true;
process.Start();
process.OutputDataReceived += new DataReceivedEventHandler(Process_OutputDataReceived);
Console.Write("Press any key to continue...");
Console.ReadKey();
process.OutputDataReceived -= new DataReceivedEventHandler(Process_OutputDataReceived);
process.CloseMainWindow();
process.Close();
}
运行时,仅显示应用程序本身的控制台窗口,但[cmd.exe]
进程窗口仍然不可见。为什么会这样,我该如何改变这种行为?