Process.StartInfo
содержит свойства типа RedirectStandardOutput
.Установив этот флаг на true
, вы сможете добавить обработчик событий на batch.StartInfo.OutputDataReceived
и прослушивать любые события.Примерно так:
Edit: Вы также можете включить перенаправление ErrorOutput для получения сообщений об ошибках.
Edit: По запросуВот полностью рабочий пример.Убедитесь, что test.bat
существует.
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
public class Program {
public static void Main() {
var form = new Form {ClientSize = new Size(400, 300)};
var button = new Button {Location = new Point(0, 0), Text = "Start", Size = new Size(400, 22)};
var listBox = new ListBox {Location = new Point(0, 22), Size = new Size(400, 278)};
form.Controls.AddRange(new Control[] {button, listBox});
button.Click += (sender, eventArgs) => {
var info = new ProcessStartInfo("test.bat") {UseShellExecute = false, RedirectStandardOutput = true};
var proc = new Process {StartInfo = info, EnableRaisingEvents = true};
proc.OutputDataReceived += (obj, args) => {
if (args.Data != null) {
listBox.Items.Add(args.Data);
}
};
proc.Start();
proc.BeginOutputReadLine();
};
form.ShowDialog();
}
}