Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I'm trying to simulate cmd in vb.net, I'm current using this code:

VB
Public Class Form1
                                                                   '

    Private WithEvents MyProcess As Process
    Private Delegate Sub AppendOutputTextDelegate(ByVal text As String)

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Me.AcceptButton = ExecuteButton

        MyProcess = New Process

        With MyProcess.StartInfo
            .FileName = "CMD.EXE"
            .UseShellExecute = False
            .CreateNoWindow = True
            .RedirectStandardInput = True
            .RedirectStandardOutput = True
            .RedirectStandardError = True
        End With

        MyProcess.Start()

        MyProcess.BeginErrorReadLine()      'start async read on stderr
        MyProcess.BeginOutputReadLine()     'start async read on stdout

        AppendOutputText("Process Started at: " & MyProcess.StartTime.ToString)

    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        MyProcess.StandardInput.WriteLine("EXIT") 'send an EXIT command to the Command Prompt
        MyProcess.StandardInput.Flush()
        MyProcess.Close()

    End Sub

    Private Sub MyProcess_ErrorDataReceived(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs) Handles MyProcess.ErrorDataReceived

        AppendOutputText(vbCrLf & "Error: " & e.Data)

    End Sub

    Private Sub MyProcess_OutputDataReceived(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs) Handles MyProcess.OutputDataReceived

        AppendOutputText(vbCrLf & e.Data)

    End Sub

    Private Sub ExecuteButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExecuteButton.Click

        MyProcess.StandardInput.WriteLine(InputTextBox.Text)
        MyProcess.StandardInput.Flush()
        InputTextBox.Text = ""

    End Sub

    Private Sub AppendOutputText(ByVal text As String)

        If OutputTextBox.InvokeRequired Then
            Dim myDelegate As New AppendOutputTextDelegate(AddressOf AppendOutputText)
            Me.Invoke(myDelegate, text)
        Else
            OutputTextBox.AppendText(text)
        End If

    End Sub

End Class

This works pretty well but it doesn't automatically update, it only updates when a command is sent. How can I modify it so it updates automatically not just when a command is sent?

Thanks for any help
Posted
Comments
ZurdoDev 9-Sep-13 13:10pm    
What do you mean update automatically?
Kieran Crown 10-Sep-13 3:17am    
The output only updates when a command is sent, I want the output to be updated when ever something happens in command prompt
Member 13287683 5-Jul-17 2:16am    
Thank you
This code is vary useful to me.........

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900