开发者

I need cmd output and errors to be posted on a textblock at run time

Currently I have a command that does this. First, upon the button click event I execute a batch file and then I will create a new text file by saving the log, results and erros into the text file. I would like to create a textblock so that this textblock would shows these results and errors at during the process of running the batch file. Is it possible?

My current code for writing into a text file:

System.Diagnostics.Process runantc = new System.Diagnostics.Process();

runantc.StartInfo.FileName = "C:\\GUI\\batch.bat";

runantc.StartInfo.UseShellExecute = false;
runantc.StartInfo.RedirectStandardOutput = true;
runantc.StartInfo.RedirectStandardError = true;
runantc.Start();

string procOutput = runantc.StandardOutput.ReadToEnd();
string procError = runantc.StandardError.ReadToEnd();


// create a writer and open the file
TextWriter outputlog = new StreamWriter("C:\\GUI\\processoutput.txt");
outputlog.Write(procOutput);
outputlog.Close();

// create a writer and open the file
TextWriter outputerror = new StreamWriter("C:\\GUI\\error.开发者_JAVA百科txt");
outputerror.Write(procError);
outputerror.Close();

Edit 1: I understand the code:

string procOutput = runantc.StandardOutput.ReadToEnd();

reads out the output from start to end, not during run process so I think my question is to ask whether if I can replace this with something so that I can see it at runtime


If I understand what you want to do, in addition to writing the output to a log file, while the process is actually running, you'd like to display the output/errors to a textblock, basically allowing you to watch the process's output as it's executing. Is that right? I have not personally done this, but if it's what you want to do, then you should look at listening to the output/error asynchronously, and setting up an event handler that the proc will call when it writes output or errors. See the documentation on BeginErrorReadline at MSDN and also documentation on ErrorDataReceived A similar event handler exists for output.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜