Read and write to the command line console using VB.net

I asked a similar question elsewhere, but maybe I did not ask it correctly, or I was not clear enough, so I ask again.

This is where I want:

  • Open a Windows command prompt
  • Launch a DOS application using the dos command
  • Read the returned text that appears in the dos field and show it in the text field in my window form. This must be repeated at regular intervals (say, every second), and the dos window should not close.

I traveled around circles trying to use the Process and StartInfo commands, but they start the application and close the process immediately. I need to open a dos window and continue reading any new text that is added to it by the dos application. I also came across this thread, which seems to be responding to my problem, but it is in C #, and I was not able to convert it:

Read STDOUT Windows Command Prompt

I ended up in the part where I open the command line and launch the application, but I don’t know how to read the data that it returns to the dos window console from time to time. I want to constantly check for changes so that I can act on them, possibly using a timer.

Please, help.

Thank!

I ran the code that was kindly provided by Stevedog and used it as follows:

  Private WithEvents _commandExecutor As New CommandExecutor()

  Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    _commandExecutor.Execute("c:\progra~2\zbar\bin\zbarcam.exe", "")
  End Sub

  Private Sub _commandExecutor_OutputRead(ByVal output As String) Handles _commandExecutor.OutputRead
    txtResult.Text = output
  End Sub

, , - dos. zbarcam , , , QR-, dos, sub _commandExecutor_OutputRead , DOS.

0
2

# , , . :

Public Function ExecuteCommand(ByVal filePath As String, ByVal arguments As String) As String
    Dim p As Process
    p = New Process()
    p.StartInfo.FileName = filePath
    p.StartInfo.UseShellExecute = False
    p.StartInfo.RedirectStandardInput = True
    p.StartInfo.RedirectStandardOutput = True
    p.Start()
    p.WaitForExit()
    Return p.StandardOutput.ReadToEnd()
End Function

:

Dim output As String = ExecuteCommand("mycommand.exe", "")

, . , , , .

, , :

Public Class CommandExecutor
    Implements IDisposable

    Public Event OutputRead(ByVal output As String)

    Private WithEvents _process As Process

    Public Sub Execute(ByVal filePath As String, ByVal arguments As String)
        If _process IsNot Nothing Then
            Throw New Exception("Already watching process")
        End If
        _process = New Process()
        _process.StartInfo.FileName = filePath
        _process.StartInfo.UseShellExecute = False
        _process.StartInfo.RedirectStandardInput = True
        _process.StartInfo.RedirectStandardOutput = True
        _process.Start()
        _process.BeginOutputReadLine()
    End Sub

    Private Sub _process_OutputDataReceived(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs) Handles _process.OutputDataReceived
        If _process.HasExited Then
            _process.Dispose()
            _process = Nothing
        End If
        RaiseEvent OutputRead(e.Data)
    End Sub

    Private disposedValue As Boolean = False
    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                If _process IsNot Nothing Then
                    _process.Kill()
                    _process.Dispose()
                    _process = Nothing
                End If
            End If
        End If
        Me.disposedValue = True
    End Sub

    Public Sub Dispose() Implements IDisposable.Dispose
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
End Class

:

Public Class Form1
    Private WithEvents _commandExecutor As New CommandExecutor()

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        _commandExecutor.Execute("D:\Sandbox\SandboxSolution\ConsoleCs\bin\Debug\ConsoleCs.exe", "")
    End Sub

    Private Sub _commandExecutor_OutputRead(ByVal output As String) Handles _commandExecutor.OutputRead
        Me.Invoke(New processCommandOutputDelegate(AddressOf processCommandOutput), output)
    End Sub

    Private Delegate Sub processCommandOutputDelegate(ByVal output As String)
    Private Sub processCommandOutput(ByVal output As String)
        TextBox1.Text = output
    End Sub

    Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        _commandExecutor.Dispose()
    End Sub
End Class
+4

, .


:

Dim proc as Process

Sub RunApp
    proc = new Process()
    proc.StartInfo.FileName = "your_app.exe"
    proc.StartInfo.Arguments = ""
    proc.StartInfo.UseShellExecute = false
    proc.StartInfo.RedirectStandardInput = true
    proc.StartInfo.RedirectStandardOutput = true
    AddHandler proc.OutputDataReceived, AddressOf InterProcOutputHandler
    proc.Start()
    proc.WaitForExit()
End Sub

Sub InterProcOutputHandler(sendingProcess as object, outLine as DataReceivedEventArgs)
    'Read data here

    'Send command if necessary
    proc.StandardInput.WriteLine("your_command")
End Sub

proc.OutputDataReceived , .

, C# event += handler, VB.NET AddHandler event, AddressOf handler.

+2

All Articles