0

真的需要帮助!我想知道如何将 jar 输出到 VB 2008 中的文本框中。我还想向它发送命令(就像使用此命令时的 CMD 一样:C:\Windows\System32\java.exe -Xms128M -Xmx1024M -jar Craftbukkit.jar)

4

1 回答 1

1

下面是概念验证。您必须根据自己的愿望和愿望对其进行调整。这是做什么的:

  1. 在后台启动一个进程
  2. 开始读取输出和错误流。
  3. 您可以通过文本框发送命令(并单击按钮)。
  4. 在输出文本框中写入输出/错误。

同样,这段代码只是一个概念证明,它还远未完成(但它已经足够演示了)。您必须添加一些额外的检查等以使其“防水”。

Public Class MyForm
    Private WithEvents _CmdProcess As Process
    Private Delegate Sub DisplayTextDelegate(text As String)

    Private Sub MyForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim processInfo As New ProcessStartInfo()
        processInfo.FileName = "cmd"
        processInfo.RedirectStandardError = True
        processInfo.RedirectStandardInput = True
        processInfo.RedirectStandardOutput = True
        processInfo.UseShellExecute = False
        processInfo.CreateNoWindow = True

        _CmdProcess = Process.Start(processInfo)
        _CmdProcess.BeginOutputReadLine()
        _CmdProcess.BeginErrorReadLine()
    End Sub

    Private Sub MyForm_Disposed(sender As System.Object, e As System.EventArgs) Handles MyBase.Disposed
        If _CmdProcess IsNot Nothing Then
            _CmdProcess.Close()
        End If
        _CmdProcess = Nothing
    End Sub

    Private Sub btnExecute_Click(sender As System.Object, e As System.EventArgs) Handles btnExecute.Click
        If Not String.IsNullOrWhiteSpace(txtCommand.Text) Then
            Dim inputStream As System.IO.StreamWriter = _CmdProcess.StandardInput
            inputStream.WriteLine(txtCommand.Text)
            inputStream.Flush()
        End If
    End Sub

    Private Sub CmdProcess_ErrorDataReceived(sender As System.Object, e As System.Diagnostics.DataReceivedEventArgs) Handles _CmdProcess.ErrorDataReceived
        Invoke(New DisplayTextDelegate(AddressOf DisplayText), Environment.NewLine)
        Invoke(New DisplayTextDelegate(AddressOf DisplayText), "Error!")
        Invoke(New DisplayTextDelegate(AddressOf DisplayText), e.Data)
    End Sub

    Private Sub CmdProcess_OutputDataReceived(sender As System.Object, e As System.Diagnostics.DataReceivedEventArgs) Handles _CmdProcess.OutputDataReceived
        Invoke(New DisplayTextDelegate(AddressOf DisplayText), e.Data)
    End Sub

    Private Sub DisplayText(text As String)
        txtOutput.AppendText(Environment.NewLine)
        txtOutput.AppendText(text)
    End Sub
End Class
于 2013-08-25T13:31:08.363 回答