代码之家  ›  专栏  ›  技术社区  ›  Andrew Russell

将子进程的输出(stdout、stderr)重定向到visualstudio中的output窗口

  •  14
  • Andrew Russell  · 技术社区  · 15 年前

    目前,我正在从我的C程序启动批处理文件:

    System.Diagnostics.Process.Start(@"DoSomeStuff.bat");
    

    (另外:当子进程完成时,它不会全部缓冲起来,然后输出到输出窗口。)


    (顺便说一句:目前我可以得到的stdout(但不是stderr)的 起源 通过使我的程序成为“Windows应用程序”而不是“Console应用程序”,进程出现在输出窗口中。如果程序在visualstudio外部运行,则会中断,但在我的特定情况下,这是可以的。)

    4 回复  |  直到 15 年前
        1
  •  23
  •   Dai    8 年前
    process.StartInfo.CreateNoWindow = true;
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.RedirectStandardOutput = true;
    process.OutputDataReceived += (sender, args) => Console.WriteLine(args.Data);
    process.Start();
    process.BeginOutputReadLine();
    
    process.WaitForExit();
    

    同样的想法 Error ,只需替换 Output 在这些方法/属性名称中。

        2
  •  8
  •   UweBaemayr    14 年前

    请注意,这只是从实际代码中提取的片段,因此可能会有一些小错误。

    // Set this to your output window Pane
    private EnvDTE.OutputWindowPane _OutputPane = null;
    
    // Methods to receive standard output and standard error
    
    private static void StandardOutputReceiver(object sendingProcess, DataReceivedEventArgs outLine)
    {
       // Receives the child process' standard output
       if (! string.IsNullOrEmpty(outLine.Data)) {
           if (_OutputPane != null)
               _OutputPane.Write(outLine.Data + Environment.NewLine);
       }
    }
    
    private static void StandardErrorReceiver(object sendingProcess, DataReceivedEventArgs errLine)
    {
       // Receives the child process' standard error
       if (! string.IsNullOrEmpty(errLine.Data)) {
           if (_OutputPane != null)
               _OutputPane.Write("Error> " + errLine.Data + Environment.NewLine);
       }
    }
    
    // main code fragment
    {
        // Start the new process
        ProcessStartInfo startInfo = new ProcessStartInfo(PROGRAM.EXE);
        startInfo.Arguments = COMMANDLINE;
        startInfo.WorkingDirectory = srcDir;
        startInfo.UseShellExecute = false;
        startInfo.RedirectStandardOutput = true;
        startInfo.RedirectStandardError = true;
        startInfo.CreateNoWindow = true;
        Process p = Process.Start(startInfo);
        p.OutputDataReceived += new DataReceivedEventHandler(StandardOutputReceiver);
        p.BeginOutputReadLine();
        p.ErrorDataReceived += new DataReceivedEventHandler(StandardErrorReceiver);
        p.BeginErrorReadLine();
        bool completed = p.WaitForExit(20000);
        if (!completed)
        {
            // do something here if it didn't finish in 20 seconds
        }
        p.Close();
    }
    
        3
  •  2
  •   Roger Lipscombe    15 年前

    这里发生的是visualstudio正在输出窗口中显示程序的调试输出。也就是说:如果你使用Trace.WriteLine行,它将出现在输出窗口中,因为默认的跟踪侦听器。

    不知何故,您的Windows窗体应用程序(当它使用控制台写入线;我假设你正在使用控制台写入线)也在编写调试输出,visualstudio正在处理这个问题。

    它不会对子进程执行相同的操作,除非显式捕获输出并将其与输出一起重定向。

        4
  •  -4
  •   Igby Largeman    14 年前

    你考虑过使用 DefaultTraceListener ?

        //Create and add a new default trace listener.
        DefaultTraceListener defaultListener;
        defaultListener = new DefaultTraceListener();
        Trace.Listeners.Add(defaultListener);
    
    推荐文章