Redirecting console to textbox, last line not included...

marco208

Member
Joined
Jun 14, 2011
Messages
6
Programming Experience
Beginner
VB.NET:
 Private Sub StreamInput(ByVal Text As String)        m_Process.StandardInput.WriteLine(Text)
        m_Process.StandardInput.Flush()
    End Sub


    Private Function ConvertFromOem(ByVal Text As String) As String
        Return _
            Encoding.GetEncoding( _
                CultureInfo.InstalledUICulture.TextInfo.OEMCodePage _
            ).GetString(Encoding.Default.GetBytes(Text))
    End Function


    Private Sub StreamOutput()
        Dim Line As String = m_Process.StandardOutput.ReadLine()
        Try
            Do While Line.Length >= 0
                AddText(ConvertFromOem(Line))
                Line = m_Process.StandardOutput.ReadLine()
            Loop
        Catch
            AddText(String.Format("""{0}"" end of output!", m_Process.StartInfo.FileName))
        End Try
    End Sub


    Private Sub StreamError()
        Dim Line As String = m_Process.StandardError.ReadLine()
        Try
            Do While Line.Length >= 0
                Line = m_Process.StandardError.ReadLine()
                AddText(Line)
            Loop
        Catch
            AddText(String.Format("""{0}"" stream error!", m_Process.StartInfo.FileName))
        End Try
    End Sub


    Private Sub Testagain()
        Dim Line As String = m_Process.StandardOutput.ReadLine()
        Try
            Do While Line.Length >= 0
                If Line.Length > 0 Then
                    AddText(ConvertFromOem(Line))
                End If
                Line = m_Process.StandardOutput.ReadLine()
            Loop
        Catch
            AddText(String.Format("""{0}"" stream error!", m_Process.StartInfo.FileName))
        End Try
    End Sub


    Private Sub AddText(ByVal Text As String)
        m_TextToAdd = Text
        Me.Invoke(CType(AddressOf Me.AddTextToTextBox, MethodInvoker))
    End Sub


    Private Sub AddTextToTextBox()
        Me.txtConsole.AppendText(m_TextToAdd & ControlChars.NewLine)
        Me.txtConsole.SelectionStart = Me.txtConsole.Text.Length
    End Sub




    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        m_Process = New Process
        With m_Process.StartInfo
            .FileName = "cmd"
            .UseShellExecute = False
            .CreateNoWindow = True
            .RedirectStandardOutput = True
            .RedirectStandardError = True
            .RedirectStandardInput = True
        End With
        m_Process.Start()


        m_OutputThread = New Thread(AddressOf StreamOutput)
        m_OutputThread.IsBackground = True
        m_OutputThread.Start()
        m_ErrorThread = New Thread(AddressOf StreamError)
        m_ErrorThread.IsBackground = True
        m_ErrorThread.Start()


    End Sub
    Private Sub btnSend_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSend.Click
        StreamInput(Me.txtCommandInput.Text)
        Me.txtCommandInput.Clear()
    End Sub


    Private Sub MainForm_Closing(ByVal sender As Object, ByVal e As CancelEventArgs) Handles MyBase.Closing
        If Not m_Process.HasExited Then
            m_Process.Kill()
        End If
        m_Process.Close()
    End Sub



This script starts an CMD screen and redirects all of its content to my textbox, but the last line always seems tobe gone...

vb.png

Here a screenshot, its missing the C:\users\marco\.....> line...

how to get that line?


Thanks,

Greets,
Marco
 
The last line (first line actually) is not yet a complete line because it has not been terminated. ReadLine will block until it detects a cr/lf char or combination that means a end of line is reached. If you need that you need use other Read methods than ReadLine.
 
Have a look in the StreamReader documentation, there are several option for reading other than ReadLine.
 
Back
Top