locked
FAQs: 如何重定向一个进程的标准I/O?_ RRS feed

答案

  • 你需要使用Process,设定其StandardInputStandardOutput属性来实现其操作。请看以下代码段。

     

    Imports System.IO

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            Dim p As New Process()

            Dim sw As StreamWriter

            Dim sr As StreamReader

            Dim err As StreamReader

            Dim psI As New ProcessStartInfo("cmd")

            psI.UseShellExecute = False

            psI.RedirectStandardInput = True

            psI.RedirectStandardOutput = True

            psI.RedirectStandardError = True

            psI.CreateNoWindow = True

            p.StartInfo = psI

            p.Start()

            sw = p.StandardInput

            sr = p.StandardOutput

            err = p.StandardError

            sw.AutoFlush = True

            If TextBox3.Text <> "" Then

                sw.WriteLine(TextBox3.Text)

            Else

                ' 执行默认命令

                sw.WriteLine("dir \")

            End If

            sw.Close()

            TextBox1.Text = sr.ReadToEnd()

            TextBox1.Text += err.ReadToEnd()

        End Sub

    End Class

     

    详细信息请阅读以下链接:

    http://social.msdn.microsoft.com/forums/en-US/Vsexpressvb/thread/62acea83-236b-4f55-8eec-25d80df5df66/

    http://www.c-sharpcorner.com/UploadFile/edwinlima/SystemDiagnosticProcess12052005035444AM/SystemDiagnosticProcess.aspx


    如果您对我们的论坛在线支持服务有任何的意见或建议,请通过邮件告诉我们。
    MSDN 论坛好帮手 立刻免费下载  MSDN 论坛好帮手
    2011年3月2日 11:54