态示例:
是否有人可以帮助我从控制台输出中读取过程。
我有第三方控制台应用程序,他的应用程序可能不会在每行之后刷新数据,我无法阅读。
是否有其他方法,而不是使用 Process 喜欢Shell或使用Windows API在运行时读取控制台的数据?
这是我的代码:
Private Sub StartMiner()
MinerProcess = New Process
MinerProcess.StartInfo.CreateNoWindow = False
MinerProcess.StartInfo.FileName = "abc.exe"
MinerProcess.StartInfo.UseShellExecute = False
MinerProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal
MinerProcess.StartInfo.RedirectStandardOutput = True
MinerProcess.StartInfo.RedirectStandardError = True
MinerProcess.EnableRaisingEvents = True
MinerProcess.StartInfo.Arguments = "--server abc"
MinerProcess.BeginOutputReadLine()
MinerProcess.BeginErrorReadLine()
MinerProcess.Start()
MinerProcess.WaitForExit()
End Sub
Private Sub MinerProcess_OutputDataReceived(sender As Object, e As DataReceivedEventArgs) Handles MinerProcess.OutputDataReceived
If Me.InvokeRequired Then
Me.BeginInvoke(CType(Sub()
RichTextBox1.Text = RichTextBox1.Text & e.Data & Environment.NewLine
End Sub, MethodInvoker))
Else
RichTextBox1.Text = RichTextBox1.Text & e.Data & Environment.NewLine
End If
End Sub
我也尝试使用读取, beginRead , readline , readto readto ,但我无需工作。所有方法都有效,但是当我终止该过程和控制台文件的运行过程很长时,请显示输出,因此无法重新打开并重新打开以获取输出。
Dim myStreamReader As StreamReader = myProcess.StandardOutput
您可以一遍又一遍地阅读 mystreamreader ...