1

我想启动一个进程并读取标准输出,但也要让这个读取输出显示在生成进程的控制台窗口中。当前使用process.StartInfo.RedirectStandardOutput = true;组合 withBeginOutputReadLine()会导致输出不显示在控制台窗口中。这是不可取的。有谁知道如何做到这一点,或者甚至可能吗?

澄清评论。

我有一个响应进程输出的函数,我设置了:

    ProcessHandle.OutputDataReceived += new DataReceivedEventHandler(ProcessHandle_OutputDataReceived);

    void ProcessHandle_OutputDataReceived(object sender, DataReceivedEventArgs e)
    {
         ... //React to output here.
    }

但是这样做输出不会进入生成进程的控制台窗口,有没有办法手动将其反馈到该控制台,所以它显示为好像我的应用程序没有拦截它?

4

1 回答 1

0
        var pi = new ProcessStartInfo
        {
            FileName = prog,
            Arguments = args,
            UseShellExecute = false,
            CreateNoWindow = true,
            RedirectStandardOutput = true,
            RedirectStandardError = false
        };

        var proc = new Process { StartInfo = pi };
        try
        {
            if (!proc.Start())
            {
                throw new ApplicationException("Starting proc failed!");
            }

            Console.WriteLine(proc.StandardOutput.ReadToEnd());
            proc.WaitForExit();
            if (proc.ExitCode != 0)
            {
                //throw new ApplicationException(String.Format("proc returned exit code {0}", proc.ExitCode));
            }
        }
        catch (Exception ex)
        {
            throw new ApplicationException("Unknown problem in proc", ex);
        }
        finally
        {
            if (!proc.HasExited)
                proc.Kill();
        }
于 2013-06-03T20:40:19.877 回答