我需要在运行时将 cmd 输出和错误发布到文本块上
目前我有一个命令可以执行此操作。首先,在按钮单击事件时,我执行一个批处理文件,然后通过将日志、结果和错误保存到文本文件中来创建一个新的文本文件。我想创建一个文本块,以便该文本块在运行批处理文件的过程中显示这些结果和错误。是否可以?
我当前写入文本文件的代码:
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.txt");
outputerror.Write(procError);
outputerror.Close();
编辑1: 我理解代码:
string procOutput = runantc.StandardOutput.ReadToEnd();
从头到尾读出输出,而不是在运行过程中读出输出,所以我认为我的问题是问是否可以用某些东西替换它,以便我可以在运行时看到它
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.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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解您想要做什么,除了将输出写入日志文件之外,当进程实际运行时,您还希望将输出/错误显示到文本块,基本上允许您将进程的输出视为它正在执行。是这样吗?我个人没有这样做,但如果这是您想要做的,那么您应该考虑异步监听输出/错误,并设置一个事件处理程序,过程在写入输出或错误时将调用该事件处理程序。请参阅 MSDN 上的 BeginErrorReadline 文档 有关 ErrorDataReceived 的文档 存在类似的事件处理程序输出。
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.