I have spent the past three hours looking for a solution but have only found solutions that output the contents of CMD once the process has finished, not while it is still running.
My code currently is as follows:
Public Class main_form
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim process_cmd As ProcessStartInfo = New ProcessStartInfo("cmd.exe")
Dim process_obj As Process
process_cmd.CreateNoWindow = True
process_cmd.UseShellExecute = False
process_cmd.RedirectStandardInput = True
process_cmd.RedirectStandardOutput = True
process_obj = Process.Start(process_cmd)
process_obj.StandardInput.WriteLine("my command")
process_obj.StandardInput.Close()
RichTextBox1.Text = process_obj.StandardOutput.ReadToEnd
process_obj.StandardOutput.Close()
End Sub
End Class
Now this code works fine and all however it only outputs to the Richtextbox once the process in CMD has finished. I want the Richtextbox to display the contents of the CMD window in real time as well as for the process to accept additional commands (e.g status of the current process) while it is still running, as if the Richtextbox is the CMD window itself in terms of output (essentially mirroring the CMD window).
I want to do this so that i can then manipulate the contents of the output using my application and then input more parameters into the CMD process based on this. (For example running a server and changing certain parameters while it is still running.)
EDIT:
I have been almost able to accomplish my task by using redirect standard output, something which I was unaware existed until another user brought it to my attention.
Below is my new code:
Public Class main_form
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim p As New Process()
p.StartInfo.FileName = "mycmdexecutable"
p.StartInfo.Arguments = ""
p.StartInfo.WorkingDirectory = ""
p.StartInfo.RedirectStandardError = True
p.StartInfo.RedirectStandardOutput = True
p.EnableRaisingEvents = True
p.StartInfo.UseShellExecute = False
Application.DoEvents()
AddHandler p.ErrorDataReceived, AddressOf p_OutputDataReceived
AddHandler p.OutputDataReceived, AddressOf p_OutputDataReceived
p.Start()
p.BeginErrorReadLine()
p.BeginOutputReadLine()
End Sub
Delegate Sub UpdateTextBoxDelg(text As String)
Public myDelegate As UpdateTextBoxDelg = New UpdateTextBoxDelg(AddressOf UpdateTextBox)
Public Sub UpdateTextBox(text As String)
RichTextBox1.Text += text & Environment.NewLine
RichTextBox1.SelectionStart = RichTextBox1.Text.Length
RichTextBox1.ScrollToCaret()
End Sub
Public Sub p_OutputDataReceived(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
If Me.InvokeRequired = True Then
Me.Invoke(myDelegate, e.Data)
Else
UpdateTextBox(e.Data)
End If
End Sub
This code works fine for displaying the CMD output to the richtextbox I have. However two new issues have cropped up.
I'm having trouble now inputting data into the process that I am running, I have absolutely no clue as to where to start and the existing examples I have found online are too complex for me to understand.
My output into the richtextbox includes some invalid characters and characters which were not present in the CMD window (E.g '[0m'). I initially thought that this was an encoding error with the richtextbox however that does not seem to be the case and I do not know how to progress further.
Once again thank you for your help!