0

我在其他地方问过类似的问题,但也许我没有以正确的方式问它,或者我不够清楚,所以我再问一次。

这是我想要得到的地方:

  1. 打开 Windows 命令提示符
  2. 通过 dos 命令运行 DOS 应用程序
  3. 阅读 dos 框中显示的返回文本,并将其显示在我的 windows 窗体中的文本框中。这需要定期(例如,每秒)重复一次,并且不应关闭 dos 框。

我一直在尝试使用 Process 和 StartInfo 命令,但它们只运行应用程序并立即关闭进程。我需要保持 dos 框打开并继续阅读由 dos 应用程序添加到其中的任何新文本。我也遇到了这个似乎回答了我的问题的线程,但它是在 C# 中,我无法转换它:

阅读 Windows 命令提示符 STDOUT

我确实到达了打开命令提示符并启动应用程序的部分,但我不知道如何读取它不时返回到 dos 框控制台的数据。我想经常检查更改,以便我可以对它们采取行动,也许使用计时器控件。

请帮忙。

谢谢!

我运行了 Stevedog 提供的代码,并像这样使用它:

  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 框中,并且_commandExecutor_OutputReadsub 不会被触发,除非我关闭 DOS 框。

4

2 回答 2

5

该 C# 示例很糟糕,因为它没有显示如何实际读取标准输出流。创建一个这样的方法:

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
于 2012-07-16T14:19:18.730 回答
2

当涉及到事件时,这些转换器通常会失败。


该代码的直接翻译如下所示:

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

于 2012-07-16T14:04:06.223 回答