.NET C#: Execute command line batch file and process the output line by line (line break treatment)

Problem

A Batch File should be executed and the output processed line by line. This is especially useful when you want to generate line break treatments.

Ansatz – Approach

The usage of a stream reader helps to process the output line by line.

Solution – Lösung

Process p = new Process();

// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;

p.StartInfo.FileName = "D:\\doors_stats\\whatschanged.bat";
p.Start();

StreamReader sr = p.StandardOutput;
string output = "";
while (!sr.EndOfStream)
{
	// Do s.th. with the received line (i.e. concate)
	output += sr.ReadLine() + "\n\n<br/>";
}

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.