简体   繁体   English

重定向输出时,控制台窗口不可见

[英]Console window is not visible when output is redirected

I am using the following code from a .NET 4 console application: 我使用.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();
}

When run, only the console window of the app itself shows up but the [cmd.exe] process window remains invisible. 运行时,只显示应用程序本身的控制台窗口,但[cmd.exe]进程窗口仍然不可见。 Why is that and how can I change this behavior? 为什么这样,我怎么能改变这种行为?

if you set UseShellExecute = true the cmd process window will appear. 如果设置UseShellExecute = true ,将显示cmd进程窗口。 You wil need to set RedirectStandardInput and RedirectStandardpOutput to 'false' (or comment them out) however. 但是,您需要将RedirectStandardInput和RedirectStandardpOutput设置为'false'(或将其注释掉)。

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 = true;
    process.StartInfo.CreateNoWindow = false;
    process.EnableRaisingEvents = true;
    process.Start();

    process.OutputDataReceived += null;

    Console.Write("Press any key to continue...");
    Console.ReadKey();

    process.OutputDataReceived -= null;
    process.CloseMainWindow();
    process.Close();
}

I don't really know how to fix it but I know why it works like that. 我真的不知道如何修复它,但我知道为什么它会这样。

You set 你设定

UseShellExecute=false;

well cmd.exe is Windows Command Processor so executing with windows shell creates new console windows which is terminal whose input is processed by the cmd.exe and output is directed to the terminal. well cmd.exeWindows Command Processor因此使用Windows shell执行会创建新的控制台窗口,该终端的输入由cmd.exe处理,输出将定向到终端。

If you set UseShellExecute=true the window will show but you won't be able to redirect input and output and this is because of the way it works as I described in above paragraph 如果设置UseShellExecute=true ,窗口将显示,但您将无法重定向输入和输出,这是因为它的工作方式,如我在上面的段落中所描述的

EDIT: The most important part: you wrote "the process window"; 编辑:最重要的部分:你写了“过程窗口”; console type procesesses have NO WINDOW at all 控制台类型进程根本没有窗口

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM