我试图从Openfiles.exe读取输出,这是行不通的

时间:2017-12-26 21:08:08

标签: vb.net process

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

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)

1 个答案:

答案 0 :(得分:0)

这只是我的示例代码,与您尝试的类似。但是,我的应用程序尝试使用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