Option
Strict
On
Public
Class
Form1
Private
Sub
Button1_Click(
ByVal
sender
As
System.
Object
,
e
System.EventArgs)
Handles
Button1.Click
Dim
OFD
New
OpenFileDialog
With
{.Filter =
"DOS Batch Files|*.bat"
, .Multiselect =
False
}
If
OFD.ShowDialog = DialogResult.OK
Then
ExecuteBatchFile(OFD.FileName)
End
Function
ExecuteDosScript(
BatchScriptLines
List(Of
String
))
OutputString
=
.Empty
Using Process
Process
AddHandler
Process.OutputDataReceived,
(sendingProcess
, outLine
DataReceivedEventArgs)
OutputString = OutputString & outLine.Data & vbCrLf
Process.StartInfo.FileName =
"cmd"
Process.StartInfo.UseShellExecute =
Process.StartInfo.CreateNoWindow =
True
Process.StartInfo.RedirectStandardInput =
Process.StartInfo.RedirectStandardOutput =
Process.StartInfo.RedirectStandardError =
Process.Start()
Process.BeginOutputReadLine()
Using InputStream
System.IO.StreamWriter = Process.StandardInput
InputStream.AutoFlush =
For
Each
ScriptLine
In
InputStream.Write(ScriptLine & vbCrLf)
Next
Using
Do
Application.DoEvents()
Loop
Until
Process.HasExited
Return
ExecuteBatchFile(
Filename
)
Not
IO.Path.GetExtension(Filename).ToLower =
".bat"
Throw
Exception(
"Invalid batch file extension"
) = IO.File.ReadAllLines(Filename).ToList
MsgBox(ExecuteDosScript(BatchScriptLines))