vb.net读取另一程序的标准输出

本文介绍如何使用C#代码访问一个进程的标准化输出,通过实例演示了如何使用Process类启动进程并读取其输出,同时提供了将此操作放入BackgroundWorker中以避免UI阻塞的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 访问一个进程的标准化输出

 Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Dim p = New Process

        p.StartInfo.FileName = "ping"
        p.StartInfo.Arguments = "www.baidu.net"
        ' 必须禁用操作系统外壳程序  
        p.StartInfo.UseShellExecute = False
        p.StartInfo.CreateNoWindow = True
        p.StartInfo.RedirectStandardOutput = True

        p.Start()
        Dim output = p.StandardOutput.ReadToEnd()


        If (String.IsNullOrEmpty(output) = False) Then
            TextBox1.AppendText(output + vbCrLf)
        End If
        p.WaitForExit()
        p.Close()
    End Sub

在进程的waitForExit期间,主程序是被等待的。可以把进程放到后台运行,新建一个BackgroundWorker部件,在按钮按下时运行

 Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        BackgroundWorker1.RunWorkerAsync()
    End Sub

    Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Dim p = New Process

        p.StartInfo.FileName = "ping"
        p.StartInfo.Arguments = "www.baidu.net"
        ' 必须禁用操作系统外壳程序  
        p.StartInfo.UseShellExecute = False
        p.StartInfo.CreateNoWindow = True
        p.StartInfo.RedirectStandardOutput = True

        p.Start()
        Dim output = p.StandardOutput.ReadToEnd()


        If (String.IsNullOrEmpty(output) = False) Then
            Me.Invoke(Sub() TextBox1.AppendText(output + vbCrLf))
        End If
        p.WaitForExit()
        p.Close()
    End Sub

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值