I know this has been discussed a million times and I have a working program. I am allowing users to execute .bat file from a GUI. My application is simple it has a form with a split container with a list of bat file on one side and a textbox on the other. When the user clicks on a batch file the application submits it to DOS and displays the output and errors in the textbox. My problem is if I put a command that will cause an error in the first line (eg. DIRP) the error shows in the textbox "DIRP is not a valid DOS command" but if it is the last line of the bat file nothing is displayed. My concern is that the application is not getting the error if it occurs on the last command. Has anyone had experience with this and is there a solution that would insure the error is displayed. Thanks in advance for all your help.
VB.NET:
Imports System.IO
Public Class Form1
Public StartPath As String = "\\my\ExstreamGMS\VM\Prod_Bat\"
Sub New()
InitializeComponent()
ListBox1.Items.Clear()
Dim fList As String() = Directory.GetFiles(StartPath)
For Each f As String In fList
ListBox1.Items.Add(Path.GetFileName(f))
Next
End Sub
Sub Go(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
Dim psi = New ProcessStartInfo()
With psi
.FileName = StartPath & Me.ListBox1.SelectedItem
.WorkingDirectory = "C:\"
.CreateNoWindow = True
.UseShellExecute = False
.ErrorDialog = False
.RedirectStandardOutput = True
.RedirectStandardError = True
End With
Dim p As New Process With {.StartInfo = psi, .EnableRaisingEvents = True}
AddHandler p.OutputDataReceived, AddressOf HelloMum
AddHandler p.ErrorDataReceived, AddressOf HelloMum
p.Start()
p.BeginOutputReadLine()
p.BeginErrorReadLine()
End Sub
Sub HelloMum(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
UpdateTextBox(e.Data)
End Sub
Private Delegate Sub UpdateTextBoxDelegate(ByVal Text As String)
Private Sub UpdateTextBox(ByVal Tex As String)
If Me.InvokeRequired Then
Dim del As New UpdateTextBoxDelegate(AddressOf UpdateTextBox)
Dim args As Object() = {Tex}
Me.Invoke(del, args)
Else
TextBox1.Text &= Tex & Environment.NewLine
TextBox1.Select(TextBox1.TextLength, 0)
TextBox1.ScrollToCaret()
End If
End Sub
End Class