目前我正从我的C#程序启动批处理文件:
System.Diagnostics.Process.Start(@"DoSomeStuff.bat");
Run Code Online (Sandbox Code Playgroud)
我希望能够做的是将该子进程的输出(stdout和stderr)重定向到Visual Studio中的Output窗口(特别是Visual C#Express 2008).
有没有办法做到这一点?
(另外:这样就不会全部缓冲,然后在子进程完成时吐出到Output窗口.)
(顺便说一句:目前我可以通过使我的程序成为"Windows应用程序"而不是"控制台应用程序"来使父进程的stdout(但不是stderr)出现在"输出"窗口中.如果程序运行,这会中断在Visual Studio之外,但在我的特定情况下这是好的.)
从 C# 表单中,我正在运行一个进程,其启动信息类似于将控制台输出重定向到单独程序中的文本框,并且C# 在运行时获取进程输出,该进程运行正确,但输出需要很长时间才能出现在DataReceived 事件中。
我希望在流程生成后立即看到文本;根据流程标准输出无法捕获?(第一条评论)我需要等到 2 到 4 kb 的缓冲区填满后才能触发事件。
根据要求,这是代码:
void pcs_OutputDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e)
{
if (!string.IsNullOrEmpty(e.Data))
textBox1.BeginInvoke((Action)delegate { textBox1.AppendText(text + "\n"); });
}
private void LER_Go_Click(object sender, EventArgs e)
{
// variables LiDARExtRep contains the full path to an executable file
// that runs in DOS and produces verbose output.
// LER_Path.Text is the parameter passed to LiDARExtRep (only one arg for this example)
ProcessStartInfo pStartInfo …Run Code Online (Sandbox Code Playgroud)