Read Console Process Output

前端 未结 2 1341
梦毁少年i
梦毁少年i 2020-12-19 11:29

I\'m attempting to read the full contents of a console process (after 3 seconds) with the code below:

Dim NewProcess As New System.Diagnostics.Process()
With         


        
相关标签:
2条回答
  • 2020-12-19 12:18

    I would try using the Process.OutputDataReceived Event to read the output asyncronously.

    See: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.outputdatareceived.aspx#Y242

    Private Shared processOutput As StringBuilder = Nothing
    
    Public Shared Sub StartSomeProcess()
    processOutput = new StringBuilder()
    Dim NewProcess As New System.Diagnostics.Process()
    With NewProcess.StartInfo
        .FileName = EXE_PATH
        .RedirectStandardOutput = True
        .RedirectStandardError = True
        .RedirectStandardInput = True
        .UseShellExecute = False
        .WindowStyle = ProcessWindowStyle.Normal
        .CreateNoWindow = False 
    End With
    
    ' Set our event handler to asynchronously read the sort output.
    AddHandler NewProcess.OutputDataReceived, AddressOf OutputHandler
    NewProcess.Start()
    NewProcess.BeginOutputReadLine()
    NewProcess.WaitForExit()
    MsgBox(processOutput.ToString())
    End Sub
    
    Private Shared Sub OutputHandler(sendingProcess As Object, outLine As DataReceivedEventArgs)    
             ' Collect the sort command output.
             If Not String.IsNullOrEmpty(outLine.Data) Then    
                ' Add the text to the collected output.
                processOutput.AppendLine(outLine.Data)
             End If
          End Sub 
    
    0 讨论(0)
  • 2020-12-19 12:19

    'For capturing the output and error

        AddHandler NewProcess.OutputDataReceived, AddressOf OutputHandler
        AddHandler NewProcess.ErrorDataReceived, AddressOf OutputHandler
    
        NewProcess.Start()
        NewProcess.BeginOutputReadLine()
        NewProcess.BeginErrorReadLine()
    
    0 讨论(0)
提交回复
热议问题