【问题标题】:I am trying to read the Output from the Openfiles.exe and this is not working我正在尝试从 Openfiles.exe 读取输出,但这不起作用
【发布时间】:2017-12-26 21:08:08
【问题描述】:

这是运行 openfiles.exe 并从中返回输出 如果我在命令行运行 openfiles.exe 它会按预期工作 当我在这里运行它时没有错误,但我在消息框中什么也没有

Dim NewProcess As New Process()
    With NewProcess.StartInfo
        .FileName = "openfiles.exe"
        .Arguments = "/query /s FakeServer/fo csv /V /U FakeDomain\Fakeuser/P pword"
        .RedirectStandardOutput = True
        .RedirectStandardError = True
        .RedirectStandardInput = True
        .UseShellExecute = False
        .WindowStyle = ProcessWindowStyle.Normal
        .CreateNoWindow = False
    End With

    NewProcess.Start()

    System.Threading.Thread.Sleep(5000)

    MsgBox(NewProcess.StandardOutput.ReadToEnd)

【问题讨论】:

  • 一些应用程序(例如 FFmpeg)写入错误流而不是输出流。你也试过MsgBox(NewProcess.StandardError.ReadToEnd())吗?

标签: vb.net process


【解决方案1】:

这只是我拥有的示例代码,与您尝试做的类似。但是,我的应用程序试图使用 cmd.exe 运行命令并将结果实时显示到文本框。您可以尝试进行相应的修改。

Dim cmd As New Process()
Dim strCommandLine As String = "Echo Hello World"

cmd.StartInfo.FileName = "cmd.exe"
cmd.StartInfo.RedirectStandardError = True
cmd.StartInfo.RedirectStandardInput = True
cmd.StartInfo.RedirectStandardOutput = True
cmd.StartInfo.CreateNoWindow = True
cmd.StartInfo.UseShellExecute = False
cmd.EnableRaisingEvents = True
Application.DoEvents()

AddHandler cmd.ErrorDataReceived, AddressOf OutputHandler
AddHandler cmd.OutputDataReceived, AddressOf OutputHandler

cmd.Start()

cmd.StandardInput.WriteLine(strCommandLine)
cmd.StandardInput.Flush()
cmd.StandardInput.Close()
cmd.BeginErrorReadLine()
cmd.BeginOutputReadLine()
cmd.Close()

Delegate Sub UpdateTextBoxDelg(text As String)
Public myDelegate As UpdateTextBoxDelg = New UpdateTextBoxDelg(AddressOf UpdateTextBox)

Public Sub UpdateTextBox(text As String)
    txtOutput.Text += text & Environment.NewLine
    txtOutput.SelectionStart = txtOutput.Text.Length
    txtOutput.ScrollToCaret()
End Sub

Private Sub OutputHandler(sender As Object, e As DataReceivedEventArgs)
    If Me.InvokeRequired = True Then
        Me.Invoke(myDelegate, e.Data)
    Else
        UpdateTextBox(e.Data)
    End If
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多