I've been successful at making some code that takes the output from a batch or cmd and puts it on a richtextbox. The user can also send commands. The problem is that I lost some of my work resulting in me rebuilding this part of my program. Can anyone tell me what I'm doing wrong with the rebuild here's the code. I'll check back later.
Code:Public Class Command Private WithEvents MyProcess As Process Private Delegate Sub AppendOutputTextDelegate(ByVal text As String) Private Sub Command_Load(sender As Object, e As EventArgs) Handles MyBase.Load 'Start and stop button toggles go here if needed If My.Settings.SafeMode = True Then OutputComBox.Text = ("Safe Mode Enabled") TxtSendCom.Text = ("Safe Mode") ElseIf My.Settings.SafeMode = False Call OpenPro() End If End Sub Public Sub OpenPro() MyProcess = New Process With MyProcess.StartInfo .FileName = (My.Settings.SLoc) .UseShellExecute = False .CreateNoWindow = True .RedirectStandardInput = True .RedirectStandardOutput = True .RedirectStandardError = True End With MyProcess.Start() MyProcess.BeginErrorReadLine() MyProcess.BeginOutputReadLine() Dim output As String = MyProcess.StandardOutput.ReadLine() AppendOutputText("Server Started at: " & MyProcess.StartTime.ToString) End Sub Private Sub Close_Process() If MyProcess.HasExited = False Then MyProcess.WaitForExit() MyProcess.Close() End If End Sub Private Sub TxtSendCom_KeyDown(sender As Object, e As KeyEventArgs) Handles TxtSendCom.KeyDown If e.KeyCode = Keys.Enter Then ComSendBut.PerformClick() End If End Sub Private Sub Command_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing If My.Settings.SafeMode = False Then If MyProcess.HasExited = False Then TxtSendCom.Clear() TxtSendCom.Text = ("stop") ComSendBut.PerformClick() MyProcess.WaitForExit() MyProcess.Close() End If End If End Sub Private Sub MyProcess_OutputDataReceived(sender As Object, e As DataReceivedEventArgs) AppendOutputText(vbCrLf & e.Data) End Sub Private Sub MyProcess_ErrorDataReceived(sender As Object, e As DataReceivedEventArgs) MyProcess.StandardInput.Flush() AppendOutputText(vbCrLf & e.Data) MyProcess.Close() End Sub Private Sub AppendOutputText(ByVal text As String) If OutputComBox.InvokeRequired Then Dim myDelegate As New AppendOutputTextDelegate(AddressOf AppendOutputText) Invoke(myDelegate, text) Else OutputComBox.AppendText(text) End If End Sub Private Sub ComSendBut_Click(sender As Object, e As EventArgs) Handles ComSendBut.Click If My.Settings.SafeMode = False Then If TxtSendCom.Text = Nothing Then TxtSendCom.Text = ("Error, You have not typed a command!") Else MyProcess.StandardInput.WriteLine(OutputComBox.Text) MyProcess.StandardInput.Flush() TxtSendCom.Text = Nothing End If Else : OutputComBox.Text = (OutputComBox.Text & vbNewLine & TxtSendCom.Text) End If End Sub End Class




Reply With Quote
