在VB.NET中,可以使用Process
类来启动一个外部进程,并通过StandardOutput
属性来读取进程的标准输出。为了确保在进程关闭之前读取所有内容,可以使用Process.WaitForExit()
方法等待进程结束。
以下是一个示例代码:
Dim process As New Process()
process.StartInfo.FileName = "your_executable_file.exe"
process.StartInfo.UseShellExecute = False
process.StartInfo.RedirectStandardOutput = True
process.Start()
' 等待进程结束
process.WaitForExit()
' 读取标准输出
Dim output As String = process.StandardOutput.ReadToEnd()
' 输出结果
Console.WriteLine(output)
请将your_executable_file.exe
替换为你要执行的可执行文件的路径。在上述代码中,process.StandardOutput.ReadToEnd()
方法会一直阻塞,直到进程结束并标准输出被完全读取。
注意:如果外部进程不会自动关闭,你可能需要使用process.Kill()
方法来手动终止进程,以避免无限等待。
上一篇:标准输出不为每行打印日期和时间
下一篇:标准输出和标准错误