简体   繁体   English

麻烦在C#中处理批处理文件

[英]trouble to handle batch file in c#

my code is : 我的代码是:

Process myProcess = new Process();
myProcess.StartInfo.FileName = @"batchfile.bat";
myProcess.StartInfo.Arguments = "some argument";
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.StartInfo.RedirectStandardInput = true;
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess.Start();
StreamReader sr = myProcess.StandardOutput;
textBox1.Text = sr.ReadToEnd();

myProcess.WaitForExit();

it works but i want to get asynchronous output. 它有效,但是我想获得异步输出。

thanks for any help. 谢谢你的帮助。

Basically, you need to enable asynchronous read operations on StandardOutput . 基本上,您需要在StandardOutput上启用异步读取操作。

To start asynchronous read operations, you must redirect the StandardOutput stream of your batch, add an event handler to the OutputDataReceived event, and call BeginOutputReadLine . 要开始异步读取操作,必须重定向批处理的StandardOutput流,将事件处理程序添加到OutputDataReceived事件,然后调用BeginOutputReadLine Thereafter, the OutputDataReceived event signals each time the batchwrites a line to the redirected StandardOutput stream. 此后,每次批量将一行写入重定向的StandardOutput流时,都会发出OutputDataReceived事件信号。

You will finc a complete example on MSDN . 您将在MSDN上找到一个完整的示例。

Process class also exposes a StandardInput property that allows you to send a command to the standard input of the process. Process类还公开了StandardInput属性,该属性使您可以将命令发送到流程的标准输入。

You may find this article helpfull. 您可能会发现这篇文章很有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM