0

首先,我确实花了很长时间试图找到我的问题的答案,但到目前为止,什么都没有。

这是问题所在。我必须运行带有参数的 cli 命令并将输出(实时)重定向到文本框。该命令将如下所示: smcli -n name -f "script.scr"

由于 smcli 不在系统 PATH 变量中,所以我首先需要到正确的 Dir

所以,我最终运行了如下所示的完整命令: cmd /c cd "c:\Program Files(x86)\bla\" && smcli - n name -f "script.scr"

我将在一秒钟内输入代码,但基本上,一切正常,我进入正确的目录,运行命令,它运行脚本,一切都很好。

我试图将输出重定向到不同的形式,但它根本不起作用;无论如何,这将是一个额外的问题。

问题是,当脚本运行时,它会逐步说明它在做什么并将该信息发送到输出(即恢复名称,恢复磁盘配置,......)

正如我所说,我确实得到了所有这些输出……一旦一切都完成了(即在明显冻结 5 分钟后)。不将输出重定向到文本框(即打开控制台窗口),我得到实时输出。

所以这里是现在的代码(有一点,对不起):

Private Sub ReadMDcfgToolStripButton_Click(sender As Object, e As EventArgs) Handles ReadMDcfgToolStripButton.Click
        Dim exitcode As Int32
        smcli = Environment.GetEnvironmentVariable("ProgramFiles") & "\Dell\MD Storage Manager\client\" 'SMcli.exe"
        Dim gotosmclidir As String = "cd """ & smcli & """"
        Dim smclicommand As String
            smclicommand = "smcli -n " & mdname & " -c ""save storagearray configuration file=\""" & saveFileDialog2.FileName & "\"" allconfig"";"
            cmd = gotosmclidir & " && " & smclicommand
            exitcode = RunCommandCom(cmd, "", False)
End Sub

Private Function RunCommandCom(command As String, arguments As String, permanent As Boolean) As Int32
    ' Usage:
    'RunCommandCom("DIR", "/W", true)
    'For the multiple command on one line the key are the & | && and || command connectors
    '•A & B -> execute command A, then execute command B. 
    '•A | B -> execute command A, and redirect all it's output into the input of command B.
    '•A && B -> execute command A, evaluate the errorlevel after running Command A, and if the exit code (errorlevel) is 0, only then execute command B.
    '•A || B -> execute Command A, evalutate the exit code of this command and if it's anything but 0, only then execute command B.

    TextBox1.Text = "Communication in progress..." & vbCrLf
    exitcodelbl.Text = ""
    CmdCloseBtn.Enabled = False
    ToolStrip1.Enabled = False
    CmdOutputPanel.Visible = True

    Dim p As Process = New Process()
    Dim pi As ProcessStartInfo = New ProcessStartInfo()


    pi.FileName = "cmd.exe"
    pi.Arguments = " " + If(permanent = True, "/K", "/C") + " " + command + " " + arguments
    MsgBox(pi.Arguments)
    pi.CreateNoWindow = True
    pi.UseShellExecute = False
    pi.RedirectStandardOutput = True
    pi.RedirectStandardError = True
    pi.CreateNoWindow = True
    p.StartInfo = pi
    AddHandler p.OutputDataReceived, AddressOf GotData
    p.Start()
    p.BeginOutputReadLine()

    Do Until p.HasExited
    Loop

    exitcodelbl.Text = p.ExitCode
    Select Case p.ExitCode
        Case 0
            exitcodelbl.Text += " - Command completed successfully."
        Case 1
            exitcodelbl.Text += " - Could not communicate with the Array."
        Case 9
            exitcodelbl.Text += " - Could not communicate with the Array."
        Case 14
            exitcodelbl.Text += " - Could not communicate with the Array."
        Case Else
            exitcodelbl.Text += " - Unknown code."
    End Select
    Return p.ExitCode
End Function

Sub GotData(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
    End If
End Sub

除了命令本身,其他一切都来自研究,它们只是放在一起,一方面是运行命令的方式,另一方面是重定向。

我相信问题出在我的命令上(看起来那部分非常明智)。

要么我可以让它以不同的方式完成(同样,它有效但不是实时的)或者我错过了一些微不足道的事情......?

谢谢你的帮助。

以下 tinstaafl 是更新后的代码:

Private Sub MainForm_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
    copyrightlbl.Focus()
    wait(1000)
    Await RunCommandCom("", "192.168.219.152", False) '172.16.1.55
    CmdCloseBtn.Enabled = True
End Sub

Private Async Function RunCommandCom(command As String, arguments As String, permanent As Boolean) As Task(Of Int32)
    TextBox1.Text = "Communication in progress..." & vbCrLf
    exitcodelbl.Text = ""
    CmdCloseBtn.Enabled = False
    ToolStrip1.Enabled = False
    CmdOutputPanel.Visible = True

    Dim p As Process = New Process()
    With p.StartInfo
        .WorkingDirectory = Environment.GetEnvironmentVariable("ProgramFiles") & "\Dell\MD Storage Manager\client\" 'Directory for SMcli.exe"
        .FileName = "ping.exe"
        .Arguments = command + " " + arguments
        .CreateNoWindow = True
        .UseShellExecute = False
        .RedirectStandardOutput = True
        .RedirectStandardError = True
    End With

    p.Start()

    Do Until p.HasExited
        TextBox1.Text &= Await p.StandardOutput.ReadLineAsync
    Loop
    exitcodelbl.Text = p.ExitCode
    Return p.ExitCode
End Function
4

1 回答 1

2

尝试直接将输出读取到文本框,而不是使用委托。Process的StandardOutput属性是一个流,您可以使用 ReadLineAsync 方法获取输出。像这样的东西应该工作:

pi.CreateNoWindow = True
pi.UseShellExecute = False
pi.RedirectStandardOutput = True
pi.RedirectStandardError = True
p.StartInfo = pi
p.Start()

Do Until p.HasExited
   TextBox1.Text &= Await p.StandardOutput.ReadLineAsync
Loop

Intellisense 将提示您使子例程异步,但是您应该得到您想要的输出。

Private Async Function RunCommandCom(command As String, arguments As String, permanent As Boolean) As Task(Of Int32)


Await RunCommandCom("", "192.168.219.152", False) 
于 2013-10-24T17:10:14.607 回答